How to log Error in Swift
Issue #439
Use localizedDescription
We need to provide NSLocalizedDescriptionKey
inside user info dictionary, otherwise the outputt string may not be what we want.
NSError
https://developer.apple.com/documentation/foundation/nserror/1414418-localizeddescription
A string containing the localized description of the error.
The object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code.
1 | let error = NSError(domain: "com.onmyway133.MyApp", code: 2, userInfo: [ |
Error
https://developer.apple.com/documentation/swift/error/2292912-localizeddescription
Retrieve the localized description for this error.
1 | enum AppError: Error { |
Use describing String
To have better control, we can have toString
which prints closly to what we expect
1 | extension Error { |