Given that I have the following Swift error:
Error Domain=SKErrorDomain Code=0 "UNKNOWN_ERROR" UserInfo={NSLocalizedDescription=UNKNOWN_ERROR, NSUnderlyingError=0x600002dfa430 {Error Domain=ASDErrorDomain Code=500 "Unhandled exception" UserInfo={NSUnderlyingError=0x600002dfa490 {Error Domain=AMSErrorDomain Code=100 "Authentication Failed" UserInfo={NSMultipleUnderlyingErrorsKey=(
"Error Domain=AMSErrorDomain Code=2 \"An unknown error occurred. Please try again.\" UserInfo={NSLocalizedDescription=An unknown error occurred. Please try again.}",
"Error Domain=AKAuthenticationError Code=-7003 \"(null)\""
), NSLocalizedDescription=Authentication Failed, NSLocalizedFailureReason=The authentication failed.}}, NSLocalizedFailureReason=An unknown error occurred, NSLocalizedDescription=Unhandled exception}}}
The localizedDescription
for this error is UNKNOWN_ERROR
, however I would like to retrieve the underlying error called Authentication Failed
.
How can I retrieve this description in Swift?
The following code gives the last underlying error.
extension NSError {
var lastUnderlyingErrorDescription: String {
if let underlyingError = userInfo[NSUnderlyingErrorKey] as? NSError {
return underlyingError.lastUnderlyingErrorDescription // Recursion 😎
}
// LocalizedFailureReason is often - but not always - more expressive than localizedDescription.
return localizedFailureReason ?? localizedDescription
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.