311

I am defining a custom error type with Swift 3 syntax and I want to provide a user-friendly description of the error which is returned by the localizedDescription property of the Error object. How can I do it?

public enum MyError: Error {
  case customError

  var localizedDescription: String {
    switch self {
    case .customError:
      return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
    }
  }
}

let error: Error = MyError.customError
error.localizedDescription
// "The operation couldn’t be completed. (MyError error 0.)"

Is there a way for the localizedDescription to return my custom error description ("A user-friendly description of the error.")? Note that the error object here is of type Error and not MyError. I can, of course, cast the object to MyError

(error as? MyError)?.localizedDescription

but is there a way to make it work without casting to my error type?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Evgenii
  • 36,389
  • 27
  • 134
  • 170

7 Answers7

567

As described in the Xcode 8 beta 6 release notes,

Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol.

In your case:

public enum MyError: Error {
    case customError
}

extension MyError: LocalizedError {
    public var errorDescription: String? {
        switch self {
        case .customError:
            return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
        }
    }
}

let error: Error = MyError.customError
print(error.localizedDescription) // A user-friendly description of the error.

You can provide even more information if the error is converted to NSError (which is always possible):

extension MyError : LocalizedError {
    public var errorDescription: String? {
        switch self {
        case .customError:
            return NSLocalizedString("I failed.", comment: "")
        }
    }
    public var failureReason: String? {
        switch self {
        case .customError:
            return NSLocalizedString("I don't know why.", comment: "")
        }
    }
    public var recoverySuggestion: String? {
        switch self {
        case .customError:
            return NSLocalizedString("Switch it off and on again.", comment: "")
        }
    }
}

let error = MyError.customError as NSError
print(error.localizedDescription)        // I failed.
print(error.localizedFailureReason)      // Optional("I don\'t know why.")
print(error.localizedRecoverySuggestion) // Optional("Switch it off and on again.")

By adopting the CustomNSError protocol the error can provide a userInfo dictionary (and also a domain and code). Example:

extension MyError: CustomNSError {

    public static var errorDomain: String {
        return "myDomain"
    }

    public var errorCode: Int {
        switch self {
        case .customError:
            return 999
        }
    }

    public var errorUserInfo: [String : Any] {
        switch self {
        case .customError:
            return [ "line": 13]
        }
    }
}

let error = MyError.customError as NSError

if let line = error.userInfo["line"] as? Int {
    print("Error in line", line) // Error in line 13
}

print(error.code) // 999
print(error.domain) // myDomain
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 11
    Is there a reason why you make `MyError` an `Error` first and extend it with `LocalizedError` later? Is there a difference if you made it a `LocalizedError` in the first place? – Gerald Eersteling Mar 07 '17 at 09:54
  • 12
    @Gee.E: It makes no difference. It is just a way to organize the code (one extension for each protocol). Compare http://stackoverflow.com/questions/36263892/extensions-in-my-own-custom-class, http://stackoverflow.com/questions/40502086/how-to-properly-use-class-extensions-in-swift, or https://www.natashatherobot.com/using-swift-extensions/. – Martin R Mar 07 '17 at 10:08
  • 4
    Ah, check. I get what you're saying now. The "Protocol Conformance" section on https://www.natashatherobot.com/using-swift-extensions/ is indeed a good example of what you mean. Thanks! – Gerald Eersteling Mar 07 '17 at 10:12
  • 1
    @MartinR If my error would be converted to NSError how can I pass a dictionary from error that can be accessed as NSError's userInfo ? – BangOperator Mar 10 '17 at 07:30
  • 31
    Beware to type `var errorDescription: String?` instead of **`String`**. There is a bug in the implementation of LocalizedError. See [SR-5858](https://bugs.swift.org/browse/SR-5858). – ethanhuang13 Jan 18 '18 at 08:58
  • Shouldn't you still get `localizedDescription` even if you don't implement `public var errorDescription: String?`. [Docs](https://developer.apple.com/documentation/foundation/localizederror) say it has a default implementation. However when I do `print(error.localizedDescription)` I get **The operation couldn’t be completed** error from the compiler. Doing `print(error)` is fine – mfaani Sep 14 '18 at 19:03
  • Thanks! [Undocumented](https://developer.apple.com/documentation/swift/error/2292912-localizeddescription) magic is always fun to work out, so answers like this are a huge help. – Raphael Oct 15 '18 at 11:51
  • 1
    Just how do all these work? I mean the variable name we use to log is different from the variable name that we actually wrote the value in. It must be a read-only computed property that we're accessing right? – mfaani Dec 29 '18 at 13:27
  • 1
    @Honey: There is a lot of “magic” in the Error<->NSError bridging, compare https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror- bridging.md. The implementation (I think) is in https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSError.swift. – Martin R Dec 29 '18 at 17:06
  • Updated link from @MartinR's comment: https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md – Theo Nov 11 '19 at 17:02
  • @ethanhuang13 Even though it's filed as a bug, it doesn't count as a bug. LocalizedError requires the type of `errorDescription` to be `String?`, not `String`. Otherwise, the default implementation ("The operation couldn’t be completed") will be provided. With that said, I appreciate you pointing that out; I've made that mistake myself multiple times. – Peter Schorn Aug 27 '20 at 06:12
  • @Honey "The operation couldn’t be completed" **IS** the default implementation of `localizedDescription` when you either don't implement `errorDescription` or it returns `nil`. – Peter Schorn Aug 27 '20 at 06:15
61

I would also add, if your error has parameters like this

enum NetworkError: LocalizedError {
  case responseStatusError(status: Int, message: String)
}

you can call these parameters in your localized description like this:

extension NetworkError {
  public var errorDescription: String? {
    switch self {
    case let .responseStatusError(status, message):
      return "Error with status \(status) and message \(message) was thrown"
  }
}
Alexander Volkov
  • 7,904
  • 1
  • 47
  • 44
Reza Shirazian
  • 2,303
  • 1
  • 22
  • 30
11

There are now two Error-adopting protocols that your error type can adopt in order to provide additional information to Objective-C — LocalizedError and CustomNSError. Here's an example error that adopts both of them:

enum MyBetterError : CustomNSError, LocalizedError {
    case oops

    // domain
    static var errorDomain : String { return "MyDomain" }
    // code
    var errorCode : Int { return -666 }
    // userInfo
    var errorUserInfo: [String : Any] { return ["Hey":"Ho"] };

    // localizedDescription
    var errorDescription: String? { return "This sucks" }
    // localizedFailureReason
    var failureReason: String? { return "Because it sucks" }
    // localizedRecoverySuggestion
    var recoverySuggestion: String? { return "Give up" }

}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    Can you make an edit? Your examples don't help much to understand the value of each. Or just delete it because MartinR's answer offers this exactly... – mfaani Dec 29 '18 at 13:29
8

This one worked for me:

NSError(domain: "com.your", code: 0, userInfo: [NSLocalizedDescriptionKey: "Error description"])
Rico Nguyen
  • 341
  • 6
  • 11
3

Using a struct can be an alternative. A little bit elegance with static localization:

import Foundation

struct MyError: LocalizedError, Equatable {

   private var description: String!

   init(description: String) {
       self.description = description
   }

   var errorDescription: String? {
       return description
   }

   public static func ==(lhs: MyError, rhs: MyError) -> Bool {
       return lhs.description == rhs.description
   }
}

extension MyError {

   static let noConnection = MyError(description: NSLocalizedString("No internet connection",comment: ""))
   static let requestFailed = MyError(description: NSLocalizedString("Request failed",comment: ""))
}

func throwNoConnectionError() throws {
   throw MyError.noConnection
}

do {
   try throwNoConnectionError()
}
catch let myError as MyError {
   switch myError {
   case .noConnection:
       print("noConnection: \(myError.localizedDescription)")
   case .requestFailed:
       print("requestFailed: \(myError.localizedDescription)")
   default:
      print("default: \(myError.localizedDescription)")
   }
}
Zafer Sevik
  • 191
  • 1
  • 5
1

Here is more elegant solution:

  enum ApiError: String, LocalizedError {

    case invalidCredentials = "Invalid credentials"
    case noConnection = "No connection"

    var localizedDescription: String { return NSLocalizedString(self.rawValue, comment: "") }

  }
Vitalii Gozhenko
  • 9,220
  • 2
  • 48
  • 66
  • 5
    This may be more elegant at runtime, but the static localization step will fail to extract these strings for translators; you'll see a `"Bad entry in file – Argument is not a literal string"` error when you run `exportLocalizations` or `genstrings` to create your list of translatable text. – savinola Dec 19 '17 at 18:06
  • @savinola agree, static localization will not work in such case. Perhaps using `switch + case` is only option... – Vitalii Gozhenko Dec 20 '17 at 17:23
  • Using raw values will also prevent the use of associated values for any of your errors – Brody Robertson Feb 03 '19 at 18:18
1

enum NetworkError: LocalizedError {
    case noConnection

    public var description: String {
    ///You can switch self here if you have multiple cases.
        return "No internet connection"
    }

    // You need to implement `errorDescription`, not `localizedDescription`.
    public var errorDescription: String? {
        return description
    }
}
Abuzeid
  • 962
  • 10
  • 14