1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2014 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
23 Struct that describes the error codes within the Realm error domain.
24 The values can be used to catch a variety of _recoverable_ errors, especially those
25 happening when initializing a Realm instance.
31 } catch Realm.Error.incompatibleLockFile {
32 print("Realm Browser app may be attached to Realm on device?")
37 public typealias Code = RLMError.Code
39 /// Error thrown by Realm if no other specific error is returned when a realm is opened.
40 public static let fail: Code = .fail
42 /// Error thrown by Realm for any I/O related exception scenarios when a realm is opened.
43 public static let fileAccess: Code = .fileAccess
45 /// Error thrown by Realm if the user does not have permission to open or create
46 /// the specified file in the specified access mode when the realm is opened.
47 public static let filePermissionDenied: Code = .filePermissionDenied
49 /// Error thrown by Realm if the file already exists when a copy should be written.
50 public static let fileExists: Code = .fileExists
52 /// Error thrown by Realm if no file was found when a realm was opened as
53 /// read-only or if the directory part of the specified path was not found
54 /// when a copy should be written.
55 public static let fileNotFound: Code = .fileNotFound
57 /// Error thrown by Realm if the database file is currently open in another process which
58 /// cannot share with the current process due to an architecture mismatch.
59 public static let incompatibleLockFile: Code = .incompatibleLockFile
61 /// Error thrown by Realm if a file format upgrade is required to open the file,
62 /// but upgrades were explicitly disabled.
63 public static let fileFormatUpgradeRequired: Code = .fileFormatUpgradeRequired
65 /// Error thrown by Realm if there is insufficient available address space.
66 public static let addressSpaceExhausted: Code = .addressSpaceExhausted
68 /// Error thrown by Realm if there is a schema version mismatch, so that a migration is required.
69 public static let schemaMismatch: Code = .schemaMismatch
71 /// Error thrown by Realm when attempting to open an incompatible synchronized Realm file.
73 /// This error occurs when the Realm file was created with an older version of Realm and an automatic
74 /// migration to the current version is not possible. When such an error occurs, the original file is moved
75 /// to a backup location, and future attempts to open the synchronized Realm will result in a new file being
76 /// created. If you wish to migrate any data from the backup Realm, you can open it using the provided
77 /// Realm configuration.
78 public static let incompatibleSyncedFile: Code = .incompatibleSyncedFile
81 public var code: Code {
82 return (_nsError as! RLMError).code
86 public var _nsError: NSError
89 public init(_nsError error: NSError) {
93 /// Realm configuration that can be used to open the backup copy of a Realm file
95 //// Only applicable to `incompatibleSyncedFile`. Will be `nil` for all other errors.
96 public var backupConfiguration: Realm.Configuration? {
97 let configuration = userInfo[RLMBackupRealmConfigurationErrorKey] as! RLMRealmConfiguration?
98 return configuration.map(Realm.Configuration.fromRLMRealmConfiguration)
104 // Provide bridging from errors with domain RLMErrorDomain to Error.
105 extension Realm.Error: _BridgedStoredNSError {
107 public static var _nsErrorDomain = RLMErrorDomain
112 extension Realm.Error: Equatable {}
114 /// Returns a Boolean indicating whether the errors are identical.
115 public func == (lhs: Error, rhs: Error) -> Bool {
116 return lhs._code == rhs._code
117 && lhs._domain == rhs._domain
120 // MARK: Pattern Matching
123 Pattern matching matching for `Realm.Error`, so that the instances can be used with Swift's
124 `do { ... } catch { ... }` syntax.
126 public func ~= (lhs: Realm.Error, rhs: Error) -> Bool {