forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSError.swift
480 lines (399 loc) · 15.3 KB
/
NSError.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import CoreFoundation
import Darwin
// NSError and CFError conform to the standard ErrorType protocol. Compiler
// magic allows this to be done as a "toll-free" conversion when an NSError
// or CFError is used as an ErrorType existential.
extension NSError : ErrorType {
public var _domain: String { return domain }
public var _code: Int { return code }
}
extension CFError : ErrorType {
public var _domain: String {
return CFErrorGetDomain(self) as String
}
public var _code: Int {
return CFErrorGetCode(self)
}
}
// An error value to use when an Objective-C API indicates error
// but produces a nil error object.
public enum _GenericObjCError : ErrorType {
case NilError
}
/// An intrinsic used by the runtime to create an error when an
/// Objective-C API indicates failure but produces a nil error.
@warn_unused_result
@_silgen_name("swift_allocNilObjCError")
public func _allocNilObjCError() -> ErrorType {
return _GenericObjCError.NilError
}
/// An internal protocol to represent Swift error enums that map to standard
/// Cocoa NSError domains.
public protocol _ObjectiveCBridgeableErrorType : ErrorType {
/// Produce a value of the error type corresponding to the given NSError,
/// or return nil if it cannot be bridged.
init?(_bridgedNSError: NSError)
}
/// A hook for the runtime to use _ObjectiveCBridgeableErrorType in order to
/// attempt an "errorTypeValue as? SomeError" cast.
///
/// If the bridge succeeds, the bridged value is written to the uninitialized
/// memory pointed to by 'out', and true is returned. Otherwise, 'out' is
/// left uninitialized, and false is returned.
@warn_unused_result
@_silgen_name("swift_stdlib_bridgeNSErrorToErrorType")
public func _stdlib_bridgeNSErrorToErrorType<
T : _ObjectiveCBridgeableErrorType
>(error: NSError, out: UnsafeMutablePointer<T>) -> Bool {
if let bridged = T(_bridgedNSError: error) {
out.initialize(bridged)
return true
} else {
return false
}
}
/// Helper protocol for _BridgedNSError, which used used to provide
/// default implementations.
public protocol __BridgedNSError : RawRepresentable, ErrorType {
static var _NSErrorDomain: String { get }
}
// Allow two bridged NSError types to be compared.
@warn_unused_result
public func ==<T: __BridgedNSError where T.RawValue: SignedIntegerType>(
lhs: T,
rhs: T
) -> Bool {
return lhs.rawValue.toIntMax() == rhs.rawValue.toIntMax()
}
public extension __BridgedNSError where RawValue: SignedIntegerType {
public final var _domain: String { return Self._NSErrorDomain }
public final var _code: Int { return Int(rawValue.toIntMax()) }
public init?(rawValue: RawValue) {
self = unsafeBitCast(rawValue, Self.self)
}
public init?(_bridgedNSError: NSError) {
if _bridgedNSError.domain != Self._NSErrorDomain {
return nil
}
self.init(rawValue: RawValue(IntMax(_bridgedNSError.code)))
}
public final var hashValue: Int { return _code }
}
// Allow two bridged NSError types to be compared.
@warn_unused_result
public func ==<T: __BridgedNSError where T.RawValue: UnsignedIntegerType>(
lhs: T,
rhs: T
) -> Bool {
return lhs.rawValue.toUIntMax() == rhs.rawValue.toUIntMax()
}
public extension __BridgedNSError where RawValue: UnsignedIntegerType {
public final var _domain: String { return Self._NSErrorDomain }
public final var _code: Int {
return Int(bitPattern: UInt(rawValue.toUIntMax()))
}
public init?(rawValue: RawValue) {
self = unsafeBitCast(rawValue, Self.self)
}
public init?(_bridgedNSError: NSError) {
if _bridgedNSError.domain != Self._NSErrorDomain {
return nil
}
self.init(rawValue: RawValue(UIntMax(UInt(_bridgedNSError.code))))
}
public final var hashValue: Int { return _code }
}
/// Describes a raw representable type that is bridged to a particular
/// NSError domain.
///
/// This protocol is used primarily to generate the conformance to
/// _ObjectiveCBridgeableErrorType for such an enum.
public protocol _BridgedNSError : __BridgedNSError,
_ObjectiveCBridgeableErrorType,
Hashable {
/// The NSError domain to which this type is bridged.
static var _NSErrorDomain: String { get }
}
/// Enumeration that describes the error codes within the Cocoa error
/// domain.
public struct NSCocoaError : RawRepresentable, _BridgedNSError {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
public static var _NSErrorDomain: String { return NSCocoaErrorDomain }
}
@warn_unused_result
public func ~=(match: NSCocoaError, error: ErrorType) -> Bool {
guard let cocoaError = error as? NSCocoaError else { return false }
return match.rawValue == cocoaError.rawValue
}
public extension NSCocoaError {
public static var FileNoSuchFileError: NSCocoaError {
return NSCocoaError(rawValue: 4)
}
public static var FileLockingError: NSCocoaError {
return NSCocoaError(rawValue: 255)
}
public static var FileReadUnknownError: NSCocoaError {
return NSCocoaError(rawValue: 256)
}
public static var FileReadNoPermissionError: NSCocoaError {
return NSCocoaError(rawValue: 257)
}
public static var FileReadInvalidFileNameError: NSCocoaError {
return NSCocoaError(rawValue: 258)
}
public static var FileReadCorruptFileError: NSCocoaError {
return NSCocoaError(rawValue: 259)
}
public static var FileReadNoSuchFileError: NSCocoaError {
return NSCocoaError(rawValue: 260)
}
public static var FileReadInapplicableStringEncodingError: NSCocoaError {
return NSCocoaError(rawValue: 261)
}
public static var FileReadUnsupportedSchemeError: NSCocoaError {
return NSCocoaError(rawValue: 262)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var FileReadTooLargeError: NSCocoaError {
return NSCocoaError(rawValue: 263)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var FileReadUnknownStringEncodingError: NSCocoaError {
return NSCocoaError(rawValue: 264)
}
public static var FileWriteUnknownError: NSCocoaError {
return NSCocoaError(rawValue: 512)
}
public static var FileWriteNoPermissionError: NSCocoaError {
return NSCocoaError(rawValue: 513)
}
public static var FileWriteInvalidFileNameError: NSCocoaError {
return NSCocoaError(rawValue: 514)
}
@available(OSX, introduced=10.7) @available(iOS, introduced=5.0)
public static var FileWriteFileExistsError: NSCocoaError {
return NSCocoaError(rawValue: 516)
}
public static var FileWriteInapplicableStringEncodingError: NSCocoaError {
return NSCocoaError(rawValue: 517)
}
public static var FileWriteUnsupportedSchemeError: NSCocoaError {
return NSCocoaError(rawValue: 518)
}
public static var FileWriteOutOfSpaceError: NSCocoaError {
return NSCocoaError(rawValue: 640)
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public static var FileWriteVolumeReadOnlyError: NSCocoaError {
return NSCocoaError(rawValue: 642)
}
@available(OSX, introduced=10.11) @available(iOS, unavailable)
public static var FileManagerUnmountUnknownError: NSCocoaError {
return NSCocoaError(rawValue: 768)
}
@available(OSX, introduced=10.11) @available(iOS, unavailable)
public static var FileManagerUnmountBusyError: NSCocoaError {
return NSCocoaError(rawValue: 769)
}
public static var KeyValueValidationError: NSCocoaError {
return NSCocoaError(rawValue: 1024)
}
public static var FormattingError: NSCocoaError {
return NSCocoaError(rawValue: 2048)
}
public static var UserCancelledError: NSCocoaError {
return NSCocoaError(rawValue: 3072)
}
@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
public static var FeatureUnsupportedError: NSCocoaError {
return NSCocoaError(rawValue: 3328)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var ExecutableNotLoadableError: NSCocoaError {
return NSCocoaError(rawValue: 3584)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var ExecutableArchitectureMismatchError: NSCocoaError {
return NSCocoaError(rawValue: 3585)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var ExecutableRuntimeMismatchError: NSCocoaError {
return NSCocoaError(rawValue: 3586)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var ExecutableLoadError: NSCocoaError {
return NSCocoaError(rawValue: 3587)
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public static var ExecutableLinkError: NSCocoaError {
return NSCocoaError(rawValue: 3588)
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public static var PropertyListReadCorruptError: NSCocoaError {
return NSCocoaError(rawValue: 3840)
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public static var PropertyListReadUnknownVersionError: NSCocoaError {
return NSCocoaError(rawValue: 3841)
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public static var PropertyListReadStreamError: NSCocoaError {
return NSCocoaError(rawValue: 3842)
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public static var PropertyListWriteStreamError: NSCocoaError {
return NSCocoaError(rawValue: 3851)
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public static var PropertyListWriteInvalidError: NSCocoaError {
return NSCocoaError(rawValue: 3852)
}
@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
public static var XPCConnectionInterrupted: NSCocoaError {
return NSCocoaError(rawValue: 4097)
}
@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
public static var XPCConnectionInvalid: NSCocoaError {
return NSCocoaError(rawValue: 4099)
}
@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
public static var XPCConnectionReplyInvalid: NSCocoaError {
return NSCocoaError(rawValue: 4101)
}
@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
public static var UbiquitousFileUnavailableError: NSCocoaError {
return NSCocoaError(rawValue: 4353)
}
@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
public static var UbiquitousFileNotUploadedDueToQuotaError: NSCocoaError {
return NSCocoaError(rawValue: 4354)
}
@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
public static var UbiquitousFileUbiquityServerNotAvailable: NSCocoaError {
return NSCocoaError(rawValue: 4355)
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public static var UserActivityHandoffFailedError: NSCocoaError {
return NSCocoaError(rawValue: 4608)
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public static var UserActivityConnectionUnavailableError: NSCocoaError {
return NSCocoaError(rawValue: 4609)
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public static var UserActivityRemoteApplicationTimedOutError: NSCocoaError {
return NSCocoaError(rawValue: 4610)
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public static var UserActivityHandoffUserInfoTooLargeError: NSCocoaError {
return NSCocoaError(rawValue: 4611)
}
@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
public static var CoderReadCorruptError: NSCocoaError {
return NSCocoaError(rawValue: 4864)
}
@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
public static var CoderValueNotFoundError: NSCocoaError {
return NSCocoaError(rawValue: 4865)
}
@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
public var isCoderError: Bool {
return rawValue >= 4864 && rawValue <= 4991
}
@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
public var isExecutableError: Bool {
return rawValue >= 3584 && rawValue <= 3839
}
public var isFileError: Bool {
return rawValue >= 0 && rawValue <= 1023
}
public var isFormattingError: Bool {
return rawValue >= 2048 && rawValue <= 2559
}
@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
public var isPropertyListError: Bool {
return rawValue >= 3840 && rawValue <= 4095
}
@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
public var isUbiquitousFileError: Bool {
return rawValue >= 4352 && rawValue <= 4607
}
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
public var isUserActivityError: Bool {
return rawValue >= 4608 && rawValue <= 4863
}
public var isValidationError: Bool {
return rawValue >= 1024 && rawValue <= 2047
}
@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
public var isXPCConnectionError: Bool {
return rawValue >= 4096 && rawValue <= 4224
}
}
/// Enumeration that describes the error codes within the NSURL error
/// domain.
@objc public enum NSURLError : Int, _BridgedNSError {
case Unknown = -1
case Cancelled = -999
case BadURL = -1000
case TimedOut = -1001
case UnsupportedURL = -1002
case CannotFindHost = -1003
case CannotConnectToHost = -1004
case NetworkConnectionLost = -1005
case DNSLookupFailed = -1006
case HTTPTooManyRedirects = -1007
case ResourceUnavailable = -1008
case NotConnectedToInternet = -1009
case RedirectToNonExistentLocation = -1010
case BadServerResponse = -1011
case UserCancelledAuthentication = -1012
case UserAuthenticationRequired = -1013
case ZeroByteResource = -1014
case CannotDecodeRawData = -1015
case CannotDecodeContentData = -1016
case CannotParseResponse = -1017
case FileDoesNotExist = -1100
case FileIsDirectory = -1101
case NoPermissionsToReadFile = -1102
case SecureConnectionFailed = -1200
case ServerCertificateHasBadDate = -1201
case ServerCertificateUntrusted = -1202
case ServerCertificateHasUnknownRoot = -1203
case ServerCertificateNotYetValid = -1204
case ClientCertificateRejected = -1205
case ClientCertificateRequired = -1206
case CannotLoadFromNetwork = -2000
case CannotCreateFile = -3000
case CannotOpenFile = -3001
case CannotCloseFile = -3002
case CannotWriteToFile = -3003
case CannotRemoveFile = -3004
case CannotMoveFile = -3005
case DownloadDecodingFailedMidStream = -3006
case DownloadDecodingFailedToComplete = -3007
@available(OSX, introduced=10.7) @available(iOS, introduced=3.0)
case InternationalRoamingOff = -1018
@available(OSX, introduced=10.7) @available(iOS, introduced=3.0)
case CallIsActive = -1019
@available(OSX, introduced=10.7) @available(iOS, introduced=3.0)
case DataNotAllowed = -1020
@available(OSX, introduced=10.7) @available(iOS, introduced=3.0)
case RequestBodyStreamExhausted = -1021
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case BackgroundSessionRequiresSharedContainer = -995
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case BackgroundSessionInUseByAnotherProcess = -996
@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case BackgroundSessionWasDisconnected = -997
public static var _NSErrorDomain: String { return NSURLErrorDomain }
}
extension POSIXError : _BridgedNSError {
public static var _NSErrorDomain: String { return NSPOSIXErrorDomain }
}
extension MachError : _BridgedNSError {
public static var _NSErrorDomain: String { return NSMachErrorDomain }
}