Skip to content

Commit a428452

Browse files
committed
[SE-0112] Implement ErrorProtocol.localizedDescription.
Provides a localized description for an error, using Cocoa's error-handling logic. The first part of item (4) in SE-0112's proposed solution.
1 parent b0f9317 commit a428452

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ public extension ErrorProtocol where Self : CustomNSError {
135135
var _code: Int { return self.errorCode }
136136
}
137137

138+
public extension ErrorProtocol {
139+
/// Retrieve the localized description for this error.
140+
var localizedDescription: String {
141+
return (self as! NSError).localizedDescription
142+
}
143+
}
144+
138145
/// Retrieve the default userInfo dictionary for a given error.
139146
@_silgen_name("swift_Foundation_getErrorDefaultUserInfo")
140147
public func _swift_Foundation_getErrorDefaultUserInfo(_ error: ErrorProtocol)

test/1_stdlib/ErrorProtocolBridging.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ ErrorProtocolBridgingTests.test("Customizing NSError via protocols") {
417417
nsError.userInfo[NSLocalizedRecoverySuggestionErrorKey] as? String)
418418
expectOptionalEqual("there is no help when writing tests",
419419
nsError.userInfo[NSHelpAnchorErrorKey] as? String)
420+
expectEqual(nsError.localizedDescription, "something went horribly wrong")
421+
expectEqual(error.localizedDescription, "something went horribly wrong")
420422

421423
// RecoverableError
422424
expectOptionalEqual(["Delete 'throw'", "Disable the test" ],

0 commit comments

Comments
 (0)