forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoundationErrors.swift
203 lines (152 loc) · 10.6 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.
public var NSFileNoSuchFileError: Int { return CocoaError.Code.fileNoSuchFile.rawValue }
/// Failure to get a lock on file.
public var NSFileLockingError: Int { return CocoaError.Code.fileLocking.rawValue }
/// Read error, reason unknown.
public var NSFileReadUnknownError: Int { return CocoaError.Code.fileReadUnknown.rawValue }
/// Read error because of a permission problem.
public var NSFileReadNoPermissionError: Int { return CocoaError.Code.fileReadNoPermission.rawValue }
/// Read error because of an invalid file name.
public var NSFileReadInvalidFileNameError: Int { return CocoaError.Code.fileReadInvalidFileName.rawValue }
/// Read error because of a corrupted file, bad format, or similar reason.
public var NSFileReadCorruptFileError: Int { return CocoaError.Code.fileReadCorruptFile.rawValue }
/// Read error because no such file was found.
public var NSFileReadNoSuchFileError: Int { return CocoaError.Code.fileReadNoSuchFile.rawValue }
/// Read error because the string encoding was not applicable.
///
/// Access the bad encoding from the `userInfo` dictionary using
/// the `NSStringEncodingErrorKey` key.
public var NSFileReadInapplicableStringEncodingError: Int { return CocoaError.Code.fileReadInapplicableStringEncoding.rawValue }
/// Read error because the specified URL scheme is unsupported.
public var NSFileReadUnsupportedSchemeError: Int { return CocoaError.Code.fileReadUnsupportedScheme.rawValue }
/// Read error because the specified file was too large.
public var NSFileReadTooLargeError: Int { return CocoaError.Code.fileReadTooLarge.rawValue }
/// Read error because the string coding of the file could not be determined.
public var NSFileReadUnknownStringEncodingError: Int { return CocoaError.Code.fileReadUnknownStringEncoding.rawValue }
/// Write error, reason unknown.
public var NSFileWriteUnknownError: Int { return CocoaError.Code.fileWriteUnknown.rawValue }
/// Write error because of a permission problem.
public var NSFileWriteNoPermissionError: Int { return CocoaError.Code.fileWriteNoPermission.rawValue }
/// Write error because of an invalid file name.
public var NSFileWriteInvalidFileNameError: Int { return CocoaError.Code.fileWriteInvalidFileName.rawValue }
/// Write error returned when `FileManager` class’s copy, move,
/// and link methods report errors when the destination file already exists.
public var NSFileWriteFileExistsError: Int { return CocoaError.Code.fileWriteFileExists.rawValue }
/// Write error because the string encoding was not applicable.
///
/// Access the bad encoding from the `userInfo` dictionary
/// using the `NSStringEncodingErrorKey` key.
public var NSFileWriteInapplicableStringEncodingError: Int { return CocoaError.Code.fileWriteInapplicableStringEncoding.rawValue }
/// Write error because the specified URL scheme is unsupported.
public var NSFileWriteUnsupportedSchemeError: Int { return CocoaError.Code.fileWriteUnsupportedScheme.rawValue }
/// Write error because of a lack of disk space.
public var NSFileWriteOutOfSpaceError: Int { return CocoaError.Code.fileWriteOutOfSpace.rawValue }
/// Write error because because the volume is read only.
public var NSFileWriteVolumeReadOnlyError: Int { return CocoaError.Code.fileWriteVolumeReadOnly.rawValue }
public var NSFileManagerUnmountUnknownError: Int { return CocoaError.Code.fileManagerUnmountUnknown.rawValue }
public var NSFileManagerUnmountBusyError: Int { return CocoaError.Code.fileManagerUnmountBusy.rawValue }
/// Key-value coding validation error.
public var NSKeyValueValidationError: Int { return CocoaError.Code.keyValueValidation.rawValue }
/// Formatting error (related to display of data).
public var NSFormattingError: Int { return CocoaError.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.
public var NSUserCancelledError: Int { return CocoaError.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`.
public var NSFeatureUnsupportedError: Int { return CocoaError.Code.featureUnsupported.rawValue }
/// Executable is of a type that is not loadable in the current process.
public var NSExecutableNotLoadableError: Int { return CocoaError.Code.executableNotLoadable.rawValue }
/// Executable does not provide an architecture compatible with
/// the current process.
public var NSExecutableArchitectureMismatchError: Int { return CocoaError.Code.executableArchitectureMismatch.rawValue }
/// Executable has Objective-C runtime information incompatible
/// with the current process.
public var NSExecutableRuntimeMismatchError: Int { return CocoaError.Code.executableRuntimeMismatch.rawValue }
/// Executable cannot be loaded for some other reason, such as
/// a problem with a library it depends on.
public var NSExecutableLoadError: Int { return CocoaError.Code.executableLoad.rawValue }
/// Executable fails due to linking issues.
public var NSExecutableLinkError: Int { return CocoaError.Code.executableLink.rawValue }
/// An error was encountered while parsing the property list.
public var NSPropertyListReadCorruptError: Int { return CocoaError.Code.propertyListReadCorrupt.rawValue }
/// The version number of the property list is unable to be determined.
public var NSPropertyListReadUnknownVersionError: Int { return CocoaError.Code.propertyListReadUnknownVersion.rawValue }
/// An stream error was encountered while reading the property list.
public var NSPropertyListReadStreamError: Int { return CocoaError.Code.propertyListReadStream.rawValue }
/// An stream error was encountered while writing the property list.
public var NSPropertyListWriteStreamError: Int { return CocoaError.Code.propertyListWriteStream.rawValue }
public var NSPropertyListWriteInvalidError: Int { return CocoaError.Code.propertyListWriteInvalid.rawValue }
/// The XPC connection was interrupted.
public var NSXPCConnectionInterrupted: Int { return CocoaError.Code.xpcConnectionInterrupted.rawValue }
/// The XPC connection was invalid.
public var NSXPCConnectionInvalid: Int { return CocoaError.Code.xpcConnectionInvalid.rawValue }
/// The XPC connection reply was invalid.
public var NSXPCConnectionReplyInvalid: Int { return CocoaError.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.
public var NSUbiquitousFileUnavailableError: Int { return CocoaError.Code.ubiquitousFileUnavailable.rawValue }
/// The item could not be uploaded to iCloud because it would make
/// the account go over its quota.
public var NSUbiquitousFileNotUploadedDueToQuotaError: Int { return CocoaError.Code.ubiquitousFileNotUploadedDueToQuota.rawValue }
/// Connecting to the iCloud servers failed.
public var NSUbiquitousFileUbiquityServerNotAvailable: Int { return CocoaError.Code.ubiquitousFileUbiquityServerNotAvailable.rawValue }
public var NSUserActivityHandoffFailedError: Int { return CocoaError.Code.userActivityHandoffFailed.rawValue }
public var NSUserActivityConnectionUnavailableError: Int { return CocoaError.Code.userActivityConnectionUnavailable.rawValue }
public var NSUserActivityRemoteApplicationTimedOutError: Int { return CocoaError.Code.userActivityRemoteApplicationTimedOut.rawValue }
public var NSUserActivityHandoffUserInfoTooLargeError: Int { return CocoaError.Code.userActivityHandoffUserInfoTooLarge.rawValue }
public var NSCoderReadCorruptError: Int { return CocoaError.Code.coderReadCorrupt.rawValue }
public var NSCoderValueNotFoundError: Int { return CocoaError.Code.coderValueNotFound.rawValue }
#if os(OSX) || os(iOS)
import Darwin
#elseif os(Linux) || CYGWIN
import Glibc
#endif
internal func _NSErrorWithErrno(_ posixErrno : Int32, reading : Bool, path : String? = nil, url : URL? = nil, extraUserInfo : [String : Any]? = nil) -> NSError {
var cocoaError : 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
}
}
var userInfo = extraUserInfo ?? [String : Any]()
if let path = path {
userInfo[NSFilePathErrorKey] = path._nsObject
} else if let url = url {
userInfo[NSURLErrorKey] = url
}
return NSError(domain: NSCocoaErrorDomain, code: cocoaError.rawValue, userInfo: userInfo)
}