Skip to content

Commit bcb7858

Browse files
committed
Match Darwin implementation of NSError.localizedDescription
1 parent 27207fe commit bcb7858

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Foundation/NSError.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,30 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
127127
}
128128

129129
open var localizedDescription: String {
130-
let desc = userInfo[NSLocalizedDescriptionKey] as? String
131-
132-
return desc ?? "The operation could not be completed"
130+
if let localizedDescription = userInfo[NSLocalizedDescriptionKey] as? String {
131+
return localizedDescription
132+
} else {
133+
// placeholder values
134+
return "The operation could not be completed." + " " + (self.localizedFailureReason ?? "(\(domain) error \(code).)")
135+
}
133136
}
134137

135138
open var localizedFailureReason: String? {
136-
return userInfo[NSLocalizedFailureReasonErrorKey] as? String
139+
140+
if let localizedFailureReason = userInfo[NSLocalizedFailureReasonErrorKey] as? String {
141+
return localizedFailureReason
142+
} else {
143+
switch domain {
144+
case NSPOSIXErrorDomain:
145+
return String(cString: strerror(Int32(code)), encoding: .ascii)
146+
case NSCocoaErrorDomain:
147+
return nil
148+
case NSURLErrorDomain:
149+
return nil
150+
default:
151+
return nil
152+
}
153+
}
137154
}
138155

139156
open var localizedRecoverySuggestion: String? {

0 commit comments

Comments
 (0)