Search code examples
swifterror-handlingswift2

Converting ErrorType to NSError loses associated objects


In Swift 2.0 NSError conforms to the ErrorType protocol.

For a customly defined error, we can specify the associating object(s) for some cases, like below.

enum LifeError: ErrorType {
    case BeBorn
    case LostJob(job: String)
    case GetCaughtByWife(wife: String)
    ...
}

We can comfortably do the following:

do {
    try haveAffairWith(otherPerson)
} catch LifeError.GetCaughtByWife(let wife) {
    ...
}

However if we want it to pass into other places as an NSError, it loses its associating object information.

println("\(LifeError.GetCaughtByWife("Name") as NSError)")

prints:

Error Domain=... Code=1 "The operation couldn't be completed". (... error 1)

and its userInfo is nil.

Where is my wife associated with the ErrorType?


Solution

  • New in Xcode 8: CustomNSError protocol.

    enum LifeError: CustomNSError {
        case beBorn
        case lostJob(job: String)
        case getCaughtByWife(wife: String)
    
        static var errorDomain: String {
            return "LifeError"
        }
    
        var errorCode: Int {
            switch self {
            case .beBorn:
                return 0
            case .lostJob(_):
                return 1
            case .getCaughtByWife(_):
                return 2
            }
        }
    
        var errorUserInfo: [String : AnyObject] {
            switch self {
            case .beBorn:
                return [:]
            case .lostJob(let job):
                return ["Job": job]
            case .getCaughtByWife(let wife):
                return ["Wife": wife]
            }
        }
    }