Error.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. import Realm
  19. extension Realm {
  20. /**
  21. Struct that describes the error codes within the Realm error domain.
  22. The values can be used to catch a variety of _recoverable_ errors, especially those
  23. happening when initializing a Realm instance.
  24. ```swift
  25. let realm: Realm?
  26. do {
  27. realm = try Realm()
  28. } catch Realm.Error.incompatibleLockFile {
  29. print("Realm Browser app may be attached to Realm on device?")
  30. }
  31. ```
  32. */
  33. public struct Error {
  34. public typealias Code = RLMError.Code
  35. /// Error thrown by Realm if no other specific error is returned when a realm is opened.
  36. public static let fail: Code = .fail
  37. /// Error thrown by Realm for any I/O related exception scenarios when a realm is opened.
  38. public static let fileAccess: Code = .fileAccess
  39. /// Error thrown by Realm if the user does not have permission to open or create
  40. /// the specified file in the specified access mode when the realm is opened.
  41. public static let filePermissionDenied: Code = .filePermissionDenied
  42. /// Error thrown by Realm if the file already exists when a copy should be written.
  43. public static let fileExists: Code = .fileExists
  44. /// Error thrown by Realm if no file was found when a realm was opened as
  45. /// read-only or if the directory part of the specified path was not found
  46. /// when a copy should be written.
  47. public static let fileNotFound: Code = .fileNotFound
  48. /// Error thrown by Realm if the database file is currently open in another process which
  49. /// cannot share with the current process due to an architecture mismatch.
  50. public static let incompatibleLockFile: Code = .incompatibleLockFile
  51. /// Error thrown by Realm if a file format upgrade is required to open the file,
  52. /// but upgrades were explicitly disabled.
  53. public static let fileFormatUpgradeRequired: Code = .fileFormatUpgradeRequired
  54. /// Error thrown by Realm if there is insufficient available address space.
  55. public static let addressSpaceExhausted: Code = .addressSpaceExhausted
  56. /// Error thrown by Realm if there is a schema version mismatch, so that a migration is required.
  57. public static let schemaMismatch: Code = .schemaMismatch
  58. /// Error thrown by Realm when attempting to open an incompatible synchronized Realm file.
  59. ///
  60. /// This error occurs when the Realm file was created with an older version of Realm and an automatic
  61. /// migration to the current version is not possible. When such an error occurs, the original file is moved
  62. /// to a backup location, and future attempts to open the synchronized Realm will result in a new file being
  63. /// created. If you wish to migrate any data from the backup Realm, you can open it using the provided
  64. /// Realm configuration.
  65. public static let incompatibleSyncedFile: Code = .incompatibleSyncedFile
  66. /// :nodoc:
  67. public var code: Code {
  68. return (_nsError as! RLMError).code
  69. }
  70. /// :nodoc:
  71. public let _nsError: NSError
  72. /// :nodoc:
  73. public init(_nsError error: NSError) {
  74. _nsError = error
  75. }
  76. /// Realm configuration that can be used to open the backup copy of a Realm file
  77. ///
  78. //// Only applicable to `incompatibleSyncedFile`. Will be `nil` for all other errors.
  79. public var backupConfiguration: Realm.Configuration? {
  80. let configuration = userInfo[RLMBackupRealmConfigurationErrorKey] as! RLMRealmConfiguration?
  81. return configuration.map(Realm.Configuration.fromRLMRealmConfiguration)
  82. }
  83. }
  84. }
  85. /// :nodoc:
  86. // Provide bridging from errors with domain RLMErrorDomain to Error.
  87. extension Realm.Error: _BridgedStoredNSError {
  88. /// :nodoc:
  89. public static let _nsErrorDomain = RLMErrorDomain
  90. /// :nodoc:
  91. public static let errorDomain = RLMErrorDomain
  92. }
  93. // MARK: Equatable
  94. extension Realm.Error: Equatable {}
  95. /// Returns a Boolean indicating whether the errors are identical.
  96. public func == (lhs: Error, rhs: Error) -> Bool {
  97. return lhs._code == rhs._code
  98. && lhs._domain == rhs._domain
  99. }
  100. // MARK: Pattern Matching
  101. /**
  102. Pattern matching matching for `Realm.Error`, so that the instances can be used with Swift's
  103. `do { ... } catch { ... }` syntax.
  104. */
  105. public func ~= (lhs: Realm.Error, rhs: Error) -> Bool {
  106. return lhs == rhs
  107. }