Search code examples
swiftauth0

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?


Solution

  • The problem is you're looking at the wrong error object. There are two error objects arriving, the outer error (.touchFailed) and an inner error wrapped up inside it. The inner error is the one you want to examine. But you are not examining it!

    To see what I mean, look at this done the wrong way and the right way:

    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 <--!!!!
    }