- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathFoundationErrors.swift
203 lines (152 loc) · 10.7 KB
/
FoundationErrors.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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
/// File-system operation attempted on non-existent file.
publicvarNSFileNoSuchFileError:Int{returnCocoaError.Code.fileNoSuchFile.rawValue }
/// Failure to get a lock on file.
publicvarNSFileLockingError:Int{returnCocoaError.Code.fileLocking.rawValue }
/// Read error, reason unknown.
publicvarNSFileReadUnknownError:Int{returnCocoaError.Code.fileReadUnknown.rawValue }
/// Read error because of a permission problem.
publicvarNSFileReadNoPermissionError:Int{returnCocoaError.Code.fileReadNoPermission.rawValue }
/// Read error because of an invalid file name.
publicvarNSFileReadInvalidFileNameError:Int{returnCocoaError.Code.fileReadInvalidFileName.rawValue }
/// Read error because of a corrupted file, bad format, or similar reason.
publicvarNSFileReadCorruptFileError:Int{returnCocoaError.Code.fileReadCorruptFile.rawValue }
/// Read error because no such file was found.
publicvarNSFileReadNoSuchFileError:Int{returnCocoaError.Code.fileReadNoSuchFile.rawValue }
/// Read error because the string encoding was not applicable.
///
/// Access the bad encoding from the `userInfo` dictionary using
/// the `NSStringEncodingErrorKey` key.
publicvarNSFileReadInapplicableStringEncodingError:Int{returnCocoaError.Code.fileReadInapplicableStringEncoding.rawValue }
/// Read error because the specified URL scheme is unsupported.
publicvarNSFileReadUnsupportedSchemeError:Int{returnCocoaError.Code.fileReadUnsupportedScheme.rawValue }
/// Read error because the specified file was too large.
publicvarNSFileReadTooLargeError:Int{returnCocoaError.Code.fileReadTooLarge.rawValue }
/// Read error because the string coding of the file could not be determined.
publicvarNSFileReadUnknownStringEncodingError:Int{returnCocoaError.Code.fileReadUnknownStringEncoding.rawValue }
/// Write error, reason unknown.
publicvarNSFileWriteUnknownError:Int{returnCocoaError.Code.fileWriteUnknown.rawValue }
/// Write error because of a permission problem.
publicvarNSFileWriteNoPermissionError:Int{returnCocoaError.Code.fileWriteNoPermission.rawValue }
/// Write error because of an invalid file name.
publicvarNSFileWriteInvalidFileNameError:Int{returnCocoaError.Code.fileWriteInvalidFileName.rawValue }
/// Write error returned when `FileManager` class’s copy, move,
/// and link methods report errors when the destination file already exists.
publicvarNSFileWriteFileExistsError:Int{returnCocoaError.Code.fileWriteFileExists.rawValue }
/// Write error because the string encoding was not applicable.
///
/// Access the bad encoding from the `userInfo` dictionary
/// using the `NSStringEncodingErrorKey` key.
publicvarNSFileWriteInapplicableStringEncodingError:Int{returnCocoaError.Code.fileWriteInapplicableStringEncoding.rawValue }
/// Write error because the specified URL scheme is unsupported.
publicvarNSFileWriteUnsupportedSchemeError:Int{returnCocoaError.Code.fileWriteUnsupportedScheme.rawValue }
/// Write error because of a lack of disk space.
publicvarNSFileWriteOutOfSpaceError:Int{returnCocoaError.Code.fileWriteOutOfSpace.rawValue }
/// Write error because because the volume is read only.
publicvarNSFileWriteVolumeReadOnlyError:Int{returnCocoaError.Code.fileWriteVolumeReadOnly.rawValue }
publicvarNSFileManagerUnmountUnknownError:Int{returnCocoaError.Code.fileManagerUnmountUnknown.rawValue }
publicvarNSFileManagerUnmountBusyError:Int{returnCocoaError.Code.fileManagerUnmountBusy.rawValue }
/// Key-value coding validation error.
publicvarNSKeyValueValidationError:Int{returnCocoaError.Code.keyValueValidation.rawValue }
/// Formatting error (related to display of data).
publicvarNSFormattingError:Int{returnCocoaError.Code.formatting.rawValue }
/// The user cancelled the operation (for example, by pressing Command-period).
///
/// This code is for errors that do not require a dialog displayed and might be
/// candidates for special-casing.
publicvarNSUserCancelledError:Int{returnCocoaError.Code.userCancelled.rawValue }
/// The feature is not supported, either because the file system
/// lacks the feature, or required libraries are missing,
/// or other similar reasons.
///
/// For example, some volumes may not support a Trash folder, so these methods
/// will report failure by returning `false` or `nil` and
/// an `NSError` with `NSFeatureUnsupportedError`.
publicvarNSFeatureUnsupportedError:Int{returnCocoaError.Code.featureUnsupported.rawValue }
/// Executable is of a type that is not loadable in the current process.
publicvarNSExecutableNotLoadableError:Int{returnCocoaError.Code.executableNotLoadable.rawValue }
/// Executable does not provide an architecture compatible with
/// the current process.
publicvarNSExecutableArchitectureMismatchError:Int{returnCocoaError.Code.executableArchitectureMismatch.rawValue }
/// Executable has Objective-C runtime information incompatible
/// with the current process.
publicvarNSExecutableRuntimeMismatchError:Int{returnCocoaError.Code.executableRuntimeMismatch.rawValue }
/// Executable cannot be loaded for some other reason, such as
/// a problem with a library it depends on.
publicvarNSExecutableLoadError:Int{returnCocoaError.Code.executableLoad.rawValue }
/// Executable fails due to linking issues.
publicvarNSExecutableLinkError:Int{returnCocoaError.Code.executableLink.rawValue }
/// An error was encountered while parsing the property list.
publicvarNSPropertyListReadCorruptError:Int{returnCocoaError.Code.propertyListReadCorrupt.rawValue }
/// The version number of the property list is unable to be determined.
publicvarNSPropertyListReadUnknownVersionError:Int{returnCocoaError.Code.propertyListReadUnknownVersion.rawValue }
/// An stream error was encountered while reading the property list.
publicvarNSPropertyListReadStreamError:Int{returnCocoaError.Code.propertyListReadStream.rawValue }
/// An stream error was encountered while writing the property list.
publicvarNSPropertyListWriteStreamError:Int{returnCocoaError.Code.propertyListWriteStream.rawValue }
publicvarNSPropertyListWriteInvalidError:Int{returnCocoaError.Code.propertyListWriteInvalid.rawValue }
/// The XPC connection was interrupted.
publicvarNSXPCConnectionInterrupted:Int{returnCocoaError.Code.xpcConnectionInterrupted.rawValue }
/// The XPC connection was invalid.
publicvarNSXPCConnectionInvalid:Int{returnCocoaError.Code.xpcConnectionInvalid.rawValue }
/// The XPC connection reply was invalid.
publicvarNSXPCConnectionReplyInvalid:Int{returnCocoaError.Code.xpcConnectionReplyInvalid.rawValue }
/// The item has not been uploaded to iCloud by another device yet.
///
/// When this error occurs, you do not need to ask the system
/// to start downloading the item. The system will download the item as soon
/// as it can. If you want to know when the item becomes available,
/// use an `NSMetadataQuer`y object to monitor changes to the file’s URL.
publicvarNSUbiquitousFileUnavailableError:Int{returnCocoaError.Code.ubiquitousFileUnavailable.rawValue }
/// The item could not be uploaded to iCloud because it would make
/// the account go over its quota.
publicvarNSUbiquitousFileNotUploadedDueToQuotaError:Int{returnCocoaError.Code.ubiquitousFileNotUploadedDueToQuota.rawValue }
/// Connecting to the iCloud servers failed.
publicvarNSUbiquitousFileUbiquityServerNotAvailable:Int{returnCocoaError.Code.ubiquitousFileUbiquityServerNotAvailable.rawValue }
publicvarNSUserActivityHandoffFailedError:Int{returnCocoaError.Code.userActivityHandoffFailed.rawValue }
publicvarNSUserActivityConnectionUnavailableError:Int{returnCocoaError.Code.userActivityConnectionUnavailable.rawValue }
publicvarNSUserActivityRemoteApplicationTimedOutError:Int{returnCocoaError.Code.userActivityRemoteApplicationTimedOut.rawValue }
publicvarNSUserActivityHandoffUserInfoTooLargeError:Int{returnCocoaError.Code.userActivityHandoffUserInfoTooLarge.rawValue }
publicvarNSCoderReadCorruptError:Int{returnCocoaError.Code.coderReadCorrupt.rawValue }
publicvarNSCoderValueNotFoundError:Int{returnCocoaError.Code.coderValueNotFound.rawValue }
#if os(macOS) || os(iOS)
import Darwin
#elseif os(Linux) || CYGWIN
import Glibc
#endif
internalfunc _NSErrorWithErrno(_ posixErrno :Int32, reading :Bool, path :String?=nil, url :URL?=nil, extraUserInfo :[String:Any]?=nil)->NSError{
varcocoaError:CocoaError.Code
if reading {
switch posixErrno {
case EFBIG: cocoaError =CocoaError.fileReadTooLarge
case ENOENT: cocoaError =CocoaError.fileReadNoSuchFile
case EPERM, EACCES: cocoaError =CocoaError.fileReadNoPermission
case ENAMETOOLONG: cocoaError =CocoaError.fileReadUnknown
default: cocoaError =CocoaError.fileReadUnknown
}
}else{
switch posixErrno {
case ENOENT: cocoaError =CocoaError.fileNoSuchFile
case EPERM, EACCES: cocoaError =CocoaError.fileWriteNoPermission
case ENAMETOOLONG: cocoaError =CocoaError.fileWriteInvalidFileName
case EDQUOT, ENOSPC: cocoaError =CocoaError.fileWriteOutOfSpace
case EROFS: cocoaError =CocoaError.fileWriteVolumeReadOnly
case EEXIST: cocoaError =CocoaError.fileWriteFileExists
default: cocoaError =CocoaError.fileWriteUnknown
}
}
varuserInfo= extraUserInfo ??[String : Any]()
iflet path = path {
userInfo[NSFilePathErrorKey]= path._nsObject
}elseiflet url = url {
userInfo[NSURLErrorKey]= url
}
returnNSError(domain: NSCocoaErrorDomain, code: cocoaError.rawValue, userInfo: userInfo)
}