Skip to content

Commit 55ccc2c

Browse files
committed
Added CocoaError messages
1 parent 67b3a80 commit 55ccc2c

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Foundation/NSError.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
144144
case NSPOSIXErrorDomain:
145145
return String(cString: strerror(Int32(code)), encoding: .ascii)
146146
case NSCocoaErrorDomain:
147-
return nil
147+
return CocoaError.errorMessages[CocoaError.Code(rawValue: code)]
148148
case NSURLErrorDomain:
149149
return nil
150150
default:
@@ -610,7 +610,7 @@ public struct CocoaError : _BridgedStoredNSError {
610610
public static var _nsErrorDomain: String { return NSCocoaErrorDomain }
611611

612612
/// The error code itself.
613-
public struct Code : RawRepresentable, _ErrorCodeProtocol {
613+
public struct Code : RawRepresentable, Hashable, _ErrorCodeProtocol {
614614
public typealias _ErrorType = CocoaError
615615

616616
public let rawValue: Int
@@ -670,6 +670,43 @@ public struct CocoaError : _BridgedStoredNSError {
670670
}
671671
}
672672

673+
internal extension CocoaError {
674+
static let errorMessages = [
675+
Code(rawValue: 4): "The file doesn’t exist.",
676+
Code(rawValue: 255): "The file couldn’t be locked.",
677+
Code(rawValue: 257): "You don’t have permission.",
678+
Code(rawValue: 258): "The file name is invalid.",
679+
Code(rawValue: 259): "The file isn’t in the correct format.",
680+
Code(rawValue: 260): "The file doesn’t exist.",
681+
Code(rawValue: 261): "The specified text encoding isn’t applicable.",
682+
Code(rawValue: 262): "The specified URL type isn’t supported.",
683+
Code(rawValue: 263): "The item is too large.",
684+
Code(rawValue: 264): "The text encoding of the contents couldn’t be determined.",
685+
Code(rawValue: 513): "You don’t have permission.",
686+
Code(rawValue: 514): "The file name is invalid.",
687+
Code(rawValue: 516): "A file with the same name already exists.",
688+
Code(rawValue: 517): "The specified text encoding isn’t applicable.",
689+
Code(rawValue: 518): "The specified URL type isn’t supported.",
690+
Code(rawValue: 640): "There isn’t enough space.",
691+
Code(rawValue: 642): "The volume is read only.",
692+
Code(rawValue: 1024): "The value is invalid.",
693+
Code(rawValue: 2048): "The value is invalid.",
694+
Code(rawValue: 3072): "The operation was cancelled.",
695+
Code(rawValue: 3328): "The requested operation is not supported.",
696+
Code(rawValue: 3840): "The data is not in the correct format.",
697+
Code(rawValue: 3841): "The data is in a format that this application doesn’t understand.",
698+
Code(rawValue: 3842): "An error occurred in the source of the data.",
699+
Code(rawValue: 3851): "An error occurred in the destination for the data.",
700+
Code(rawValue: 3852): "An error occurred in the content of the data.",
701+
Code(rawValue: 4353): "The file is not available on iCloud yet.",
702+
Code(rawValue: 4354): "There isn’t enough space in your account.",
703+
Code(rawValue: 4355): "The iCloud servers might be unreachable or your settings might be incorrect.",
704+
Code(rawValue: 4864): "The data isn’t in the correct format.",
705+
Code(rawValue: 4865): "The data is missing.",
706+
Code(rawValue: 4866): "The data isn’t in the correct format."
707+
]
708+
}
709+
673710
public extension CocoaError {
674711
private var _nsUserInfo: [AnyHashable : Any] {
675712
return _nsError.userInfo

0 commit comments

Comments
 (0)