Skip to content

Match Darwin implementation of NSError.localizedDescription #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,30 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

open var localizedDescription: String {
let desc = userInfo[NSLocalizedDescriptionKey] as? String

return desc ?? "The operation could not be completed"
if let localizedDescription = userInfo[NSLocalizedDescriptionKey] as? String {
return localizedDescription
} else {
// placeholder values
return "The operation could not be completed." + " " + (self.localizedFailureReason ?? "(\(domain) error \(code).)")
}
}

open var localizedFailureReason: String? {
return userInfo[NSLocalizedFailureReasonErrorKey] as? String

if let localizedFailureReason = userInfo[NSLocalizedFailureReasonErrorKey] as? String {
return localizedFailureReason
} else {
switch domain {
case NSPOSIXErrorDomain:
return String(cString: strerror(Int32(code)), encoding: .ascii)
case NSCocoaErrorDomain:
return nil
case NSURLErrorDomain:
return nil
default:
return nil
}
}
}

open var localizedRecoverySuggestion: String? {
Expand Down Expand Up @@ -164,7 +181,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

override open var description: String {
return localizedDescription
return "Error Domain=\(domain) Code=\(code) \"\(localizedFailureReason ?? "(null)")\""
}

// -- NSObject Overrides --
Expand Down Expand Up @@ -481,7 +498,7 @@ public protocol _BridgedNSError : __BridgedNSError, RawRepresentable, _Objective

/// Describes a bridged error that stores the underlying NSError, so
/// it can be queried.
public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableError, CustomNSError, Hashable {
public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableError, CustomNSError, Hashable, CustomStringConvertible {
/// The type of an error code.
associatedtype Code: _ErrorCodeProtocol

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

public extension _BridgedStoredNSError {
var description: String {
return _nsError.description
}
}

/// Various helper implementations for _BridgedStoredNSError
extension _BridgedStoredNSError where Code: RawRepresentable, Code.RawValue: FixedWidthInteger {
public var code: Code {
Expand Down