Skip to content

Commit

Permalink
Factor _NSCocoaError into a struct whose values are split across fram…
Browse files Browse the repository at this point in the history
…eworks.

The Cocoa error domain is comprised on error codes from Foundation,
CoreData, and AppKit. Rather than try to collect all of the error
codes into a single enum in Foundation, use a struct that conforms to
ErrorType. Part of rdar://problem/20536610.

Swift SVN r28755
  • Loading branch information
DougGregor committed May 19, 2015
1 parent def3b0d commit bddf134
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 120 deletions.
1 change: 1 addition & 0 deletions stdlib/public/SDK/AppKit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_swift_library(swiftAppKit IS_SDK_OVERLAY
AppKit.swift
NSError.swift
TARGET_SDKS OSX
SWIFT_MODULE_DEPENDS ObjectiveC Foundation CoreImage
FRAMEWORK_DEPENDS AppKit)
Expand Down
23 changes: 23 additions & 0 deletions stdlib/public/SDK/AppKit/NSError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public extension _NSCocoaError {
static let TextReadInapplicableDocumentTypeError = _NSCocoaError(rawValue: 65806)
static let TextWriteInapplicableDocumentTypeError = _NSCocoaError(rawValue: 66062)
static let ServiceApplicationNotFoundError = _NSCocoaError(rawValue: 66560)
static let ServiceApplicationLaunchFailedError = _NSCocoaError(rawValue: 66561)
static let ServiceRequestTimedOutError = _NSCocoaError(rawValue: 66562)
static let ServiceInvalidPasteboardDataError = _NSCocoaError(rawValue: 66563)
static let ServiceMalformedServiceDictionaryError = _NSCocoaError(rawValue: 66564)
static let ServiceMiscellaneousError = _NSCocoaError(rawValue: 66800)
static let SharingServiceNotConfiguredError = _NSCocoaError(rawValue: 67072)

public var isServiceError: Bool {
return rawValue >= 66560 && rawValue <= 66817;
}

public var isSharingServiceError: Bool {
return rawValue >= 67072 && rawValue <= 67327;
}

public var isTextReadWriteError: Bool {
return rawValue >= 65792 && rawValue <= 66303;
}
}
1 change: 1 addition & 0 deletions stdlib/public/SDK/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_subdirectory(CloudKit)
add_subdirectory(Contacts)
add_subdirectory(CoreAudio)
add_subdirectory(CoreBluetooth)
add_subdirectory(CoreData)
add_subdirectory(CoreGraphics)
add_subdirectory(CoreImage)
add_subdirectory(CoreLocation)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/public/SDK/CoreData/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_swift_library(swiftCoreData IS_SDK_OVERLAY
CoreData.swift

SWIFT_MODULE_DEPENDS Foundation)

48 changes: 48 additions & 0 deletions stdlib/public/SDK/CoreData/CoreData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@exported import CoreData
import Foundation

public extension _NSCocoaError {
static let ManagedObjectValidationError = _NSCocoaError(rawValue: 1550)
static let ValidationMultipleErrorsError = _NSCocoaError(rawValue: 1560)
static let ValidationMissingMandatoryPropertyError = _NSCocoaError(rawValue: 1570)
static let ValidationRelationshipLacksMinimumCountError = _NSCocoaError(rawValue: 1580)
static let ValidationRelationshipExceedsMaximumCountError = _NSCocoaError(rawValue: 1590)
static let ValidationRelationshipDeniedDeleteError = _NSCocoaError(rawValue: 1600)
static let ValidationNumberTooLargeError = _NSCocoaError(rawValue: 1610)
static let ValidationNumberTooSmallError = _NSCocoaError(rawValue: 1620)
static let ValidationDateTooLateError = _NSCocoaError(rawValue: 1630)
static let ValidationDateTooSoonError = _NSCocoaError(rawValue: 1640)
static let ValidationInvalidDateError = _NSCocoaError(rawValue: 1650)
static let ValidationStringTooLongError = _NSCocoaError(rawValue: 1660)
static let ValidationStringTooShortError = _NSCocoaError(rawValue: 1670)
static let ValidationStringPatternMatchingError = _NSCocoaError(rawValue: 1680)
static let ManagedObjectContextLockingError = _NSCocoaError(rawValue: 132000)
static let PersistentStoreCoordinatorLockingError = _NSCocoaError(rawValue: 132010)
static let ManagedObjectReferentialIntegrityError = _NSCocoaError(rawValue: 133000)
static let ManagedObjectExternalRelationshipError = _NSCocoaError(rawValue: 133010)
static let ManagedObjectMergeError = _NSCocoaError(rawValue: 133020)
static let ManagedObjectConstraintMergeError = _NSCocoaError(rawValue: 133021)
static let PersistentStoreInvalidTypeError = _NSCocoaError(rawValue: 134000)
static let PersistentStoreTypeMismatchError = _NSCocoaError(rawValue: 134010)
static let PersistentStoreIncompatibleSchemaError = _NSCocoaError(rawValue: 134020)
static let PersistentStoreSaveError = _NSCocoaError(rawValue: 134030)
static let PersistentStoreIncompleteSaveError = _NSCocoaError(rawValue: 134040)
static let PersistentStoreSaveConflictsError = _NSCocoaError(rawValue: 134050)
static let CoreDataError = _NSCocoaError(rawValue: 134060)
static let PersistentStoreOperationError = _NSCocoaError(rawValue: 134070)
static let PersistentStoreOpenError = _NSCocoaError(rawValue: 134080)
static let PersistentStoreTimeoutError = _NSCocoaError(rawValue: 134090)
static let PersistentStoreUnsupportedRequestTypeError = _NSCocoaError(rawValue: 134091)
static let PersistentStoreIncompatibleVersionHashError = _NSCocoaError(rawValue: 134100)
static let MigrationError = _NSCocoaError(rawValue: 134110)
static let MigrationCancelledError = _NSCocoaError(rawValue: 134120)
static let MigrationMissingSourceModelError = _NSCocoaError(rawValue: 134130)
static let MigrationMissingMappingModelError = _NSCocoaError(rawValue: 134140)
static let MigrationManagerSourceStoreError = _NSCocoaError(rawValue: 134150)
static let MigrationManagerDestinationStoreError = _NSCocoaError(rawValue: 134160)
static let EntityMigrationPolicyError = _NSCocoaError(rawValue: 134170)
static let SQLiteError = _NSCocoaError(rawValue: 134180)
static let InferredMappingModelError = _NSCocoaError(rawValue: 134190)
static let ExternalRecordImportError = _NSCocoaError(rawValue: 134200)
}

183 changes: 63 additions & 120 deletions stdlib/public/SDK/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,116 +111,130 @@ public protocol _BridgedNSError : __BridgedNSError,

/// Enumeration that describes the error codes within the Cocoa error
/// domain.
@objc public enum _NSCocoaError : Int, _BridgedNSError {
// Foundation errors
case FileNoSuchFileError = 4
case FileLockingError = 255
case FileReadUnknownError = 256
case FileReadNoPermissionError = 257
case FileReadInvalidFileNameError = 258
case FileReadCorruptFileError = 259
case FileReadNoSuchFileError = 260
case FileReadInapplicableStringEncodingError = 261
case FileReadUnsupportedSchemeError = 262
public struct _NSCocoaError : RawRepresentable, _BridgedNSError {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public static var _NSErrorDomain: String { return NSCocoaErrorDomain }
}

public func ~=(match: _NSCocoaError, error: ErrorType) -> Bool {
guard let cocoaError = error as? _NSCocoaError else { return false }
return match.rawValue == cocoaError.rawValue
}

public extension _NSCocoaError {
static let FileNoSuchFileError = _NSCocoaError(rawValue: 4)
static let FileLockingError = _NSCocoaError(rawValue: 255)
static let FileReadUnknownError = _NSCocoaError(rawValue: 256)
static let FileReadNoPermissionError = _NSCocoaError(rawValue: 257)
static let FileReadInvalidFileNameError = _NSCocoaError(rawValue: 258)
static let FileReadCorruptFileError = _NSCocoaError(rawValue: 259)
static let FileReadNoSuchFileError = _NSCocoaError(rawValue: 260)
static let FileReadInapplicableStringEncodingError = _NSCocoaError(rawValue: 261)
static let FileReadUnsupportedSchemeError = _NSCocoaError(rawValue: 262)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case FileReadTooLargeError = 263
static let FileReadTooLargeError = _NSCocoaError(rawValue: 263)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case FileReadUnknownStringEncodingError = 264
static let FileReadUnknownStringEncodingError = _NSCocoaError(rawValue: 264)

case FileWriteUnknownError = 512
case FileWriteNoPermissionError = 513
case FileWriteInvalidFileNameError = 514
static let FileWriteUnknownError = _NSCocoaError(rawValue: 512)
static let FileWriteNoPermissionError = _NSCocoaError(rawValue: 513)
static let FileWriteInvalidFileNameError = _NSCocoaError(rawValue: 514)

@available(OSX, introduced=10.7) @available(iOS, introduced=5.0)
case FileWriteFileExistsError = 516
static let FileWriteFileExistsError = _NSCocoaError(rawValue: 516)

case FileWriteInapplicableStringEncodingError = 517
case FileWriteUnsupportedSchemeError = 518
case FileWriteOutOfSpaceError = 640
static let FileWriteInapplicableStringEncodingError = _NSCocoaError(rawValue: 517)
static let FileWriteUnsupportedSchemeError = _NSCocoaError(rawValue: 518)
static let FileWriteOutOfSpaceError = _NSCocoaError(rawValue: 640)

@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
case FileWriteVolumeReadOnlyError = 642
static let FileWriteVolumeReadOnlyError = _NSCocoaError(rawValue: 642)

@available(OSX, introduced=10.11) @available(iOS, unavailable)
case FileManagerUnmountUnknownError = 768
static let FileManagerUnmountUnknownError = _NSCocoaError(rawValue: 768)

@available(OSX, introduced=10.11) @available(iOS, unavailable)
case FileManagerUnmountBusyError = 769
static let FileManagerUnmountBusyError = _NSCocoaError(rawValue: 769)

case KeyValueValidationError = 1024
case FormattingError = 2048
case UserCancelledError = 3072
static let KeyValueValidationError = _NSCocoaError(rawValue: 1024)
static let FormattingError = _NSCocoaError(rawValue: 2048)
static let UserCancelledError = _NSCocoaError(rawValue: 3072)

@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
case FeatureUnsupportedError = 3328
static let FeatureUnsupportedError = _NSCocoaError(rawValue: 3328)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case ExecutableNotLoadableError = 3584
static let ExecutableNotLoadableError = _NSCocoaError(rawValue: 3584)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case ExecutableArchitectureMismatchError = 3585
static let ExecutableArchitectureMismatchError = _NSCocoaError(rawValue: 3585)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case ExecutableRuntimeMismatchError = 3586
static let ExecutableRuntimeMismatchError = _NSCocoaError(rawValue: 3586)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case ExecutableLoadError = 3587
static let ExecutableLoadError = _NSCocoaError(rawValue: 3587)

@available(OSX, introduced=10.5) @available(iOS, introduced=2.0)
case ExecutableLinkError = 3588
static let ExecutableLinkError = _NSCocoaError(rawValue: 3588)

@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
case PropertyListReadCorruptError = 3840
static let PropertyListReadCorruptError = _NSCocoaError(rawValue: 3840)

@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
case PropertyListReadUnknownVersionError = 3841
static let PropertyListReadUnknownVersionError = _NSCocoaError(rawValue: 3841)

@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
case PropertyListReadStreamError = 3842
static let PropertyListReadStreamError = _NSCocoaError(rawValue: 3842)

@available(OSX, introduced=10.6) @available(iOS, introduced=4.0)
case PropertyListWriteStreamError = 3851
static let PropertyListWriteStreamError = _NSCocoaError(rawValue: 3851)

@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case PropertyListWriteInvalidError = 3852
static let PropertyListWriteInvalidError = _NSCocoaError(rawValue: 3852)

@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
case XPCConnectionInterrupted = 4097
static let XPCConnectionInterrupted = _NSCocoaError(rawValue: 4097)

@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
case XPCConnectionInvalid = 4099
static let XPCConnectionInvalid = _NSCocoaError(rawValue: 4099)

@available(OSX, introduced=10.8) @available(iOS, introduced=6.0)
case XPCConnectionReplyInvalid = 4101
static let XPCConnectionReplyInvalid = _NSCocoaError(rawValue: 4101)

@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
case UbiquitousFileUnavailableError = 4353
static let UbiquitousFileUnavailableError = _NSCocoaError(rawValue: 4353)

@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
case UbiquitousFileNotUploadedDueToQuotaError = 4354
static let UbiquitousFileNotUploadedDueToQuotaError = _NSCocoaError(rawValue: 4354)

@available(OSX, introduced=10.9) @available(iOS, introduced=7.0)
case UbiquitousFileUbiquityServerNotAvailable = 4355
static let UbiquitousFileUbiquityServerNotAvailable = _NSCocoaError(rawValue: 4355)

@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case UserActivityHandoffFailedError = 4608
static let UserActivityHandoffFailedError = _NSCocoaError(rawValue: 4608)

@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case UserActivityConnectionUnavailableError = 4609
static let UserActivityConnectionUnavailableError = _NSCocoaError(rawValue: 4609)

@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case UserActivityRemoteApplicationTimedOutError = 4610
static let UserActivityRemoteApplicationTimedOutError = _NSCocoaError(rawValue: 4610)

@available(OSX, introduced=10.10) @available(iOS, introduced=8.0)
case UserActivityHandoffUserInfoTooLargeError = 4611
static let UserActivityHandoffUserInfoTooLargeError = _NSCocoaError(rawValue: 4611)

@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
case CoderReadCorruptError = 4864
static let CoderReadCorruptError = _NSCocoaError(rawValue: 4864)

@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
case CoderValueNotFoundError = 4865
static let CoderValueNotFoundError = _NSCocoaError(rawValue: 4865)


@available(OSX, introduced=10.11) @available(iOS, introduced=9.0)
Expand Down Expand Up @@ -264,77 +278,6 @@ public protocol _BridgedNSError : __BridgedNSError,
public var isXPCConnectionError: Bool {
return rawValue >= 4096 && rawValue <= 4224;
}

// CoreData errors
case ManagedObjectValidationError = 1550
case ValidationMultipleErrorsError = 1560
case ValidationMissingMandatoryPropertyError = 1570
case ValidationRelationshipLacksMinimumCountError = 1580
case ValidationRelationshipExceedsMaximumCountError = 1590
case ValidationRelationshipDeniedDeleteError = 1600
case ValidationNumberTooLargeError = 1610
case ValidationNumberTooSmallError = 1620
case ValidationDateTooLateError = 1630
case ValidationDateTooSoonError = 1640
case ValidationInvalidDateError = 1650
case ValidationStringTooLongError = 1660
case ValidationStringTooShortError = 1670
case ValidationStringPatternMatchingError = 1680
case ManagedObjectContextLockingError = 132000
case PersistentStoreCoordinatorLockingError = 132010
case ManagedObjectReferentialIntegrityError = 133000
case ManagedObjectExternalRelationshipError = 133010
case ManagedObjectMergeError = 133020
case ManagedObjectConstraintMergeError = 133021
case PersistentStoreInvalidTypeError = 134000
case PersistentStoreTypeMismatchError = 134010
case PersistentStoreIncompatibleSchemaError = 134020
case PersistentStoreSaveError = 134030
case PersistentStoreIncompleteSaveError = 134040
case PersistentStoreSaveConflictsError = 134050
case CoreDataError = 134060
case PersistentStoreOperationError = 134070
case PersistentStoreOpenError = 134080
case PersistentStoreTimeoutError = 134090
case PersistentStoreUnsupportedRequestTypeError = 134091
case PersistentStoreIncompatibleVersionHashError = 134100
case MigrationError = 134110
case MigrationCancelledError = 134120
case MigrationMissingSourceModelError = 134130
case MigrationMissingMappingModelError = 134140
case MigrationManagerSourceStoreError = 134150
case MigrationManagerDestinationStoreError = 134160
case EntityMigrationPolicyError = 134170
case SQLiteError = 134180
case InferredMappingModelError = 134190
case ExternalRecordImportError = 134200

#if os(OSX)
// AppKit errors
case TextReadInapplicableDocumentTypeError = 65806
case TextWriteInapplicableDocumentTypeError = 66062
case ServiceApplicationNotFoundError = 66560
case ServiceApplicationLaunchFailedError = 66561
case ServiceRequestTimedOutError = 66562
case ServiceInvalidPasteboardDataError = 66563
case ServiceMalformedServiceDictionaryError = 66564
case ServiceMiscellaneousError = 66800
case SharingServiceNotConfiguredError = 67072

public var isServiceError: Bool {
return rawValue >= 66560 && rawValue <= 66817;
}

public var isSharingServiceError: Bool {
return rawValue >= 67072 && rawValue <= 67327;
}

public var isTextReadWriteError: Bool {
return rawValue >= 65792 && rawValue <= 66303;
}
#endif

public static var _NSErrorDomain: String { return NSCocoaErrorDomain }
}

/// Enumeration that describes the error codes within the NSURL error
Expand Down

0 comments on commit bddf134

Please sign in to comment.