Skip to content

Commit 0f07b1d

Browse files
authored
Merge pull request #2140 from colemancda/posixErrorDescription
Match Darwin implementation of `NSError.localizedDescription`
2 parents bee431a + 67b3a80 commit 0f07b1d

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

Foundation/NSError.swift

Lines changed: 29 additions & 6 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? {
@@ -164,7 +181,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
164181
}
165182

166183
override open var description: String {
167-
return localizedDescription
184+
return "Error Domain=\(domain) Code=\(code) \"\(localizedFailureReason ?? "(null)")\""
168185
}
169186

170187
// -- NSObject Overrides --
@@ -481,7 +498,7 @@ public protocol _BridgedNSError : __BridgedNSError, RawRepresentable, _Objective
481498

482499
/// Describes a bridged error that stores the underlying NSError, so
483500
/// it can be queried.
484-
public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableError, CustomNSError, Hashable {
501+
public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableError, CustomNSError, Hashable, CustomStringConvertible {
485502
/// The type of an error code.
486503
associatedtype Code: _ErrorCodeProtocol
487504

@@ -499,6 +516,12 @@ public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableE
499516
init(_nsError error: NSError)
500517
}
501518

519+
public extension _BridgedStoredNSError {
520+
var description: String {
521+
return _nsError.description
522+
}
523+
}
524+
502525
/// Various helper implementations for _BridgedStoredNSError
503526
extension _BridgedStoredNSError where Code: RawRepresentable, Code.RawValue: FixedWidthInteger {
504527
public var code: Code {

0 commit comments

Comments
 (0)