Skip to content

Commit 9522d99

Browse files
committed
[SE-0112] Make NSCustomError.errorDomain static.
The error domain is meant to identify the type; it cannot meaningfully vary.
1 parent 5cd0630 commit 9522d99

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public extension RecoverableError {
118118
/// and user-info dictionary.
119119
public protocol CustomNSError : ErrorProtocol {
120120
/// The domain of the error.
121-
var errorDomain: String { get }
121+
static var errorDomain: String { get }
122122

123123
/// The error code within the given domain.
124124
var errorCode: Int { get }
@@ -129,7 +129,7 @@ public protocol CustomNSError : ErrorProtocol {
129129

130130
public extension ErrorProtocol where Self : CustomNSError {
131131
/// Default implementation for customized NSErrors.
132-
var _domain: String { return self.errorDomain }
132+
var _domain: String { return Self.errorDomain }
133133

134134
/// Default implementation for customized NSErrors.
135135
var _code: Int { return self.errorCode }
@@ -378,7 +378,7 @@ public extension _BridgedStoredNSError {
378378
// FIXME: Would prefer to have a clear "extract an NSError
379379
// directly" operation.
380380

381-
var errorDomain: String { return _nsError.domain }
381+
static var errorDomain: String { return _nsErrorDomain }
382382

383383
var errorCode: Int { return _nsError.code }
384384

@@ -428,6 +428,7 @@ public struct NSCocoaError : _BridgedStoredNSError {
428428
public let _nsError: NSError
429429

430430
public init(_nsError error: NSError) {
431+
precondition(error.domain == NSCocoaErrorDomain)
431432
self._nsError = error
432433
}
433434

@@ -1168,6 +1169,7 @@ public struct NSURLError : _BridgedStoredNSError {
11681169
public let _nsError: NSError
11691170

11701171
public init(_nsError error: NSError) {
1172+
precondition(error.domain == NSURLErrorDomain)
11711173
self._nsError = error
11721174
}
11731175

test/1_stdlib/ErrorProtocolBridging.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ ErrorProtocolBridgingTests.test("Thrown NSError identity is preserved") {
335335

336336
// Check errors customized via protocol.
337337
struct MyCustomizedError : ErrorProtocol {
338-
let domain: String
339338
let code: Int
340339
}
341340

@@ -358,8 +357,8 @@ extension MyCustomizedError : LocalizedError {
358357
}
359358

360359
extension MyCustomizedError : CustomNSError {
361-
var errorDomain: String {
362-
return domain
360+
static var errorDomain: String {
361+
return "custom"
363362
}
364363

365364
/// The error code within the given domain.
@@ -418,7 +417,7 @@ class RecoveryDelegate {
418417
}
419418

420419
ErrorProtocolBridgingTests.test("Customizing NSError via protocols") {
421-
let error = MyCustomizedError(domain: "custom", code: 12345)
420+
let error = MyCustomizedError(code: 12345)
422421
let nsError = error as NSError
423422

424423
// CustomNSError

0 commit comments

Comments
 (0)