首页 > 解决方案 > Get error code from enum : error in swift 4.2

问题描述

I'm using Auth0 and when using biometrics, they are returning an error but the error code is incorrect.

they have a function that returns:

return callback(.touchFailed($0!), nil)

$0 is a LAError and .touchFailed is declared as

public enum CredentialsManagerError: Error {
    case noCredentials
    case noRefreshToken
    case failedRefresh(Error)
    case touchFailed(Error)
}

$0._code has a value of -3

but in the callback function the error._code is always equals to 1

How can I retrieved the actual value of -3?

标签: swiftauth0

解决方案


问题是您正在查看错误的错误对象。有两个错误对象到达,外部错误 ( .touchFailed) 和包含在其中的内部错误。内部错误是您要检查的错误。但你不是在检查它!

要明白我的意思,看看这个错误的方法和正确的方法:

public enum CredentialsManagerError: Error {
    case noCredentials
    case noRefreshToken
    case failedRefresh(Error)
    case touchFailed(Error)
}

// let's make a `.touchFailed`
let innerError = NSError(domain: "yoho", code: -3, userInfo: nil)
let outerError = CredentialsManagerError.touchFailed(innerError)

// now let's examine them
// first, the wrong way
print(outerError._code) // 1, because it's the outer error
// now, the right way
if case let .touchFailed(what) = outerError {
    print(what._code) // -3 <--!!!!
}

推荐阅读