Skip to content

Commit 511aabc

Browse files
committed
Provide CustomNSError default implementation
Pull default CustomNSError implementation from https://github.com/apple/swift/blob/master/stdlib/public/SDK/Foundation/NSError.swift
1 parent 8715257 commit 511aabc

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Foundation/NSError.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,37 @@ public protocol CustomNSError : Error {
274274
var errorUserInfo: [String : Any] { get }
275275
}
276276

277+
public extension CustomNSError {
278+
/// Default domain of the error.
279+
static var errorDomain: String {
280+
return String(reflecting: self)
281+
}
282+
283+
/// The error code within the given domain.
284+
var errorCode: Int {
285+
return _swift_getDefaultErrorCode(self)
286+
}
287+
288+
/// The default user-info dictionary.
289+
var errorUserInfo: [String : Any] {
290+
return [:]
291+
}
292+
}
293+
294+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
295+
// The error code of Error with integral raw values is the raw value.
296+
public var errorCode: Int {
297+
return numericCast(self.rawValue)
298+
}
299+
}
300+
301+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
302+
// The error code of Error with integral raw values is the raw value.
303+
public var errorCode: Int {
304+
return numericCast(self.rawValue)
305+
}
306+
}
307+
277308
public extension Error where Self : CustomNSError {
278309
/// Default implementation for customized NSErrors.
279310
var _domain: String { return Self.errorDomain }
@@ -282,6 +313,16 @@ public extension Error where Self : CustomNSError {
282313
var _code: Int { return self.errorCode }
283314
}
284315

316+
public extension Error where Self: CustomNSError, Self: RawRepresentable, Self.RawValue: SignedInteger {
317+
/// Default implementation for customized NSErrors.
318+
var _code: Int { return self.errorCode }
319+
}
320+
321+
public extension Error where Self: CustomNSError, Self: RawRepresentable, Self.RawValue: UnsignedInteger {
322+
/// Default implementation for customized NSErrors.
323+
var _code: Int { return self.errorCode }
324+
}
325+
285326
public extension Error {
286327
/// Retrieve the localized description for this error.
287328
var localizedDescription: String {

TestFoundation/TestNSError.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@
1616
import SwiftXCTest
1717
#endif
1818

19+
struct SwiftCustomNSError: Error, CustomNSError {
20+
}
1921

2022
class TestNSError : XCTestCase {
2123

2224
static var allTests: [(String, (TestNSError) -> () throws -> Void)] {
2325
return [
2426
("test_LocalizedError_errorDescription", test_LocalizedError_errorDescription),
2527
("test_NSErrorAsError_localizedDescription", test_NSErrorAsError_localizedDescription),
28+
("test_CustomNSError_domain", test_CustomNSError_domain),
29+
("test_CustomNSError_userInfo", test_CustomNSError_userInfo),
30+
("test_CustomNSError_errorCode", test_CustomNSError_errorCode),
31+
("test_CustomNSError_errorCodeRawInt", test_CustomNSError_errorCodeRawInt),
32+
("test_CustomNSError_errorCodeRawUInt", test_CustomNSError_errorCodeRawUInt),
2633
]
2734
}
2835

@@ -40,4 +47,45 @@ class TestNSError : XCTestCase {
4047
let error = nsError as Error
4148
XCTAssertEqual(error.localizedDescription, "Localized!")
4249
}
50+
51+
func test_CustomNSError_domain() {
52+
XCTAssertEqual(SwiftCustomNSError.errorDomain, "TestFoundation.SwiftCustomNSError")
53+
}
54+
55+
func test_CustomNSError_userInfo() {
56+
let userInfo = SwiftCustomNSError().errorUserInfo
57+
XCTAssertTrue(userInfo.isEmpty)
58+
}
59+
60+
func test_CustomNSError_errorCode() {
61+
enum SwiftError : Error, CustomNSError {
62+
case zero
63+
case one
64+
case two
65+
}
66+
67+
XCTAssertEqual(SwiftCustomNSError().errorCode, 1)
68+
69+
XCTAssertEqual(SwiftError.zero.errorCode, 0)
70+
XCTAssertEqual(SwiftError.one.errorCode, 1)
71+
XCTAssertEqual(SwiftError.two.errorCode, 2)
72+
}
73+
74+
func test_CustomNSError_errorCodeRawInt() {
75+
enum SwiftError : Int, Error, CustomNSError {
76+
case minusOne = -1
77+
case fortyTwo = 42
78+
}
79+
80+
XCTAssertEqual(SwiftError.minusOne.errorCode, -1)
81+
XCTAssertEqual(SwiftError.fortyTwo.errorCode, 42)
82+
}
83+
84+
func test_CustomNSError_errorCodeRawUInt() {
85+
enum SwiftError : UInt, Error, CustomNSError {
86+
case fortyTwo = 42
87+
}
88+
89+
XCTAssertEqual(SwiftError.fortyTwo.errorCode, 42)
90+
}
4391
}

0 commit comments

Comments
 (0)