Skip to content

Commit 04b3333

Browse files
authored
Provide CustomNSError default implementation
Pull default CustomNSError implementation from https://github.com/apple/swift/blob/master/stdlib/public/SDK/Foundation/NSError.swift
1 parent 79322e9 commit 04b3333

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Foundation/NSError.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,38 @@ 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
295+
where Self: RawRepresentable, Self.RawValue: SignedInteger {
296+
// The error code of Error with integral raw values is the raw value.
297+
public var errorCode: Int {
298+
return numericCast(self.rawValue)
299+
}
300+
}
301+
302+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
303+
// The error code of Error with integral raw values is the raw value.
304+
public var errorCode: Int {
305+
return numericCast(self.rawValue)
306+
}
307+
}
308+
277309
public extension Error where Self : CustomNSError {
278310
/// Default implementation for customized NSErrors.
279311
var _domain: String { return Self.errorDomain }

0 commit comments

Comments
 (0)