added iOS source code
[wl-app.git] / iOS / Pods / RealmSwift / RealmSwift / Error.swift
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2014 Realm Inc.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 import Realm
20
21 extension Realm {
22     /**
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.
26
27      ```swift
28      let realm: Realm?
29      do {
30          realm = try Realm()
31      } catch Realm.Error.incompatibleLockFile {
32          print("Realm Browser app may be attached to Realm on device?")
33      }
34      ```
35     */
36     public struct Error {
37         public typealias Code = RLMError.Code
38
39         /// Error thrown by Realm if no other specific error is returned when a realm is opened.
40         public static let fail: Code = .fail
41
42         /// Error thrown by Realm for any I/O related exception scenarios when a realm is opened.
43         public static let fileAccess: Code = .fileAccess
44
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
48
49         /// Error thrown by Realm if the file already exists when a copy should be written.
50         public static let fileExists: Code = .fileExists
51
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
56
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
60
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
64
65         /// Error thrown by Realm if there is insufficient available address space.
66         public static let addressSpaceExhausted: Code = .addressSpaceExhausted
67
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
70
71         /// Error thrown by Realm when attempting to open an incompatible synchronized Realm file.
72         ///
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
79
80         /// :nodoc:
81         public var code: Code {
82             return (_nsError as! RLMError).code
83         }
84
85         /// :nodoc:
86         public var _nsError: NSError
87
88         /// :nodoc:
89         public init(_nsError error: NSError) {
90             _nsError = error
91         }
92
93         /// Realm configuration that can be used to open the backup copy of a Realm file
94         ///
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)
99         }
100     }
101 }
102
103 /// :nodoc:
104 // Provide bridging from errors with domain RLMErrorDomain to Error.
105 extension Realm.Error: _BridgedStoredNSError {
106     /// :nodoc:
107     public static var _nsErrorDomain = RLMErrorDomain
108 }
109
110 // MARK: Equatable
111
112 extension Realm.Error: Equatable {}
113
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
118 }
119
120 // MARK: Pattern Matching
121
122 /**
123  Pattern matching matching for `Realm.Error`, so that the instances can be used with Swift's
124  `do { ... } catch { ... }` syntax.
125 */
126 public func ~= (lhs: Realm.Error, rhs: Error) -> Bool {
127     return lhs == rhs
128 }