Is there a better way to compare errors in Swift?
This suffices for your .expired
check, without needing to define ==
:
let error: Error = ...
if case AuthenticationError.expired = error {
print("it's expired")
}
If you want to extract associated data (as in the .webRequestFailed
case), you can do this:
if case AuthenticationError.webRequestFailed(error: let innerError) = error {
print("web request failed due to \(innerError.localizedDescription)")
}
Here's my macOS playground, created in Xcode 9.2:
import Foundation
public enum AuthenticationError: Error {
case unknownError
case canceledByUser
case userOrPasswordMismatch
case unableToExtractOneTimeCode
case unableToExchangeOneTimeCodeForToken
case credentialsUnavailable
case expired
case webRequestFailed(error: Error)
}
func test(_ error: Error) {
if case AuthenticationError.expired = error {
print("it's expired; error = \(error)")
} else if case AuthenticationError.webRequestFailed(error: let innerError) = error {
print("web request failed due to \(innerError.localizedDescription); error = \(error)")
} else {
print("no match; error = \(error)")
}
}
test(AuthenticationError.expired)
test(AuthenticationError.webRequestFailed(error: AuthenticationError.credentialsUnavailable))
test(NSError(domain: NSPOSIXErrorDomain, code: Int(ENOENT), userInfo: [:]))
Details
- Swift 5.1
- Xcode 11.6 (11E708)
Solution
public func == (lhs: Error, rhs: Error) -> Bool {
guard type(of: lhs) == type(of: rhs) else { return false }
let error1 = lhs as NSError
let error2 = rhs as NSError
return error1.domain == error2.domain && error1.code == error2.code && "\(lhs)" == "\(rhs)"
}
extension Equatable where Self : Error {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs as Error == rhs as Error
}
}
Usage
enum MyError: Error { case problem, bigProblem, catastrophicException }
enum MyError2: Error, Equatable { case oops, RUUUUUN(where: String) }
var errors = [Error]()
errors.append(MyError.problem)
errors.append(MyError.catastrophicException)
errors.append(MyError2.oops)
errors.append(MyError2.RUUUUUN(where: "I don't know"))
errors.append(MyError2.RUUUUUN(where: "No matter!!!"))
errors.append(NSError(domain: "domain", code: 234, userInfo: nil))
errors.append(NSError(domain: "domain2", code: 345, userInfo: nil))
for i in 0..<errors.count {
for j in i..<errors.count {
print("==============================================")
print("Error1: \(errors[i])")
print("Error2: \(errors[j])")
let result = errors[i] == errors[j]
print("(Error1 == Error2) = \(result ? "✅" : "❌" ) ")
}
}
Console
==============================================
Error1: problem
Error2: problem
(Error1 == Error2) = ✅
==============================================
Error1: problem
Error2: catastrophicException
(Error1 == Error2) = ❌
==============================================
Error1: problem
Error2: oops
(Error1 == Error2) = ❌
==============================================
Error1: problem
Error2: RUUUUUN(where: "I don\'t know")
(Error1 == Error2) = ❌
==============================================
Error1: problem
Error2: RUUUUUN(where: "No matter!!!")
(Error1 == Error2) = ❌
==============================================
Error1: problem
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: problem
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: catastrophicException
Error2: catastrophicException
(Error1 == Error2) = ✅
==============================================
Error1: catastrophicException
Error2: oops
(Error1 == Error2) = ❌
==============================================
Error1: catastrophicException
Error2: RUUUUUN(where: "I don\'t know")
(Error1 == Error2) = ❌
==============================================
Error1: catastrophicException
Error2: RUUUUUN(where: "No matter!!!")
(Error1 == Error2) = ❌
==============================================
Error1: catastrophicException
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: catastrophicException
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: oops
Error2: oops
(Error1 == Error2) = ✅
==============================================
Error1: oops
Error2: RUUUUUN(where: "I don\'t know")
(Error1 == Error2) = ❌
==============================================
Error1: oops
Error2: RUUUUUN(where: "No matter!!!")
(Error1 == Error2) = ❌
==============================================
Error1: oops
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: oops
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: RUUUUUN(where: "I don\'t know")
Error2: RUUUUUN(where: "I don\'t know")
(Error1 == Error2) = ✅
==============================================
Error1: RUUUUUN(where: "I don\'t know")
Error2: RUUUUUN(where: "No matter!!!")
(Error1 == Error2) = ❌
==============================================
Error1: RUUUUUN(where: "I don\'t know")
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: RUUUUUN(where: "I don\'t know")
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: RUUUUUN(where: "No matter!!!")
Error2: RUUUUUN(where: "No matter!!!")
(Error1 == Error2) = ✅
==============================================
Error1: RUUUUUN(where: "No matter!!!")
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: RUUUUUN(where: "No matter!!!")
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: Error Domain=domain Code=234 "(null)"
Error2: Error Domain=domain Code=234 "(null)"
(Error1 == Error2) = ✅
==============================================
Error1: Error Domain=domain Code=234 "(null)"
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ❌
==============================================
Error1: Error Domain=domain2 Code=345 "(null)"
Error2: Error Domain=domain2 Code=345 "(null)"
(Error1 == Error2) = ✅