Error.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /// :nodoc:
  59. public var code: Code {
  60. return (_nsError as! RLMError).code
  61. }
  62. /// :nodoc:
  63. public let _nsError: NSError
  64. /// :nodoc:
  65. public init(_nsError error: NSError) {
  66. _nsError = error
  67. }
  68. /// Realm configuration that can be used to open the backup copy of a Realm file
  69. ///
  70. //// Only applicable to `incompatibleSyncedFile`. Will be `nil` for all other errors.
  71. public var backupConfiguration: Realm.Configuration? {
  72. let configuration = userInfo[RLMBackupRealmConfigurationErrorKey] as! RLMRealmConfiguration?
  73. return configuration.map(Realm.Configuration.fromRLMRealmConfiguration)
  74. }
  75. }
  76. }
  77. /// :nodoc:
  78. // Provide bridging from errors with domain RLMErrorDomain to Error.
  79. extension Realm.Error: _BridgedStoredNSError {
  80. /// :nodoc:
  81. public static let _nsErrorDomain = RLMErrorDomain
  82. /// :nodoc:
  83. public static let errorDomain = RLMErrorDomain
  84. }
  85. // MARK: Equatable
  86. extension Realm.Error: Equatable {}
  87. /// Returns a Boolean indicating whether the errors are identical.
  88. public func == (lhs: Error, rhs: Error) -> Bool {
  89. return lhs._code == rhs._code
  90. && lhs._domain == rhs._domain
  91. }
  92. // MARK: Pattern Matching
  93. /**
  94. Pattern matching matching for `Realm.Error`, so that the instances can be used with Swift's
  95. `do { ... } catch { ... }` syntax.
  96. */
  97. public func ~= (lhs: Realm.Error, rhs: Error) -> Bool {
  98. return lhs == rhs
  99. }