StorageError.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseStorageInternal
  16. /// The error domain for codes in the `StorageErrorCode` enum.
  17. public let StorageErrorDomain: String = "FIRStorageErrorDomain"
  18. public enum StorageError: Error {
  19. case unknown
  20. case objectNotFound(String)
  21. case bucketNotFound(String)
  22. case projectNotFound(String)
  23. case quotaExceeded(String)
  24. case unauthenticated
  25. case unauthorized(String, String)
  26. case retryLimitExceeded
  27. case nonMatchingChecksum
  28. case downloadSizeExceeded(Int64, Int64)
  29. case cancelled
  30. case invalidArgument(String)
  31. case internalError(String)
  32. static func swiftConvert(objcError: NSError) -> StorageError {
  33. let userInfo = objcError.userInfo
  34. switch objcError.code {
  35. case FIRIMPLStorageErrorCode.unknown.rawValue: return StorageError.unknown
  36. case FIRIMPLStorageErrorCode.objectNotFound.rawValue:
  37. guard let object = userInfo["object"] as? String else {
  38. return StorageError
  39. .internalError(
  40. "Failed to decode object not found error: \(objcError.localizedDescription)"
  41. )
  42. }
  43. return StorageError.objectNotFound(object)
  44. case FIRIMPLStorageErrorCode.bucketNotFound.rawValue:
  45. guard let bucket = userInfo["bucket"] as? String else {
  46. return StorageError
  47. .internalError(
  48. "Failed to decode bucket not found error: \(objcError.localizedDescription)"
  49. )
  50. }
  51. return StorageError.bucketNotFound(bucket)
  52. case FIRIMPLStorageErrorCode.projectNotFound.rawValue:
  53. guard let project = userInfo["project"] as? String else {
  54. return StorageError
  55. .internalError(
  56. "Failed to decode project not found error: \(objcError.localizedDescription)"
  57. )
  58. }
  59. return StorageError.projectNotFound(project)
  60. case FIRIMPLStorageErrorCode.quotaExceeded.rawValue:
  61. guard let bucket = userInfo["bucket"] as? String else {
  62. return StorageError
  63. .internalError("Failed to decode quota exceeded error: \(objcError.localizedDescription)")
  64. }
  65. return StorageError.quotaExceeded(bucket)
  66. case FIRIMPLStorageErrorCode.unauthenticated.rawValue: return StorageError.unauthenticated
  67. case FIRIMPLStorageErrorCode.unauthorized.rawValue:
  68. guard let bucket = userInfo["bucket"] as? String,
  69. let object = userInfo["object"] as? String else {
  70. return StorageError
  71. .internalError(
  72. "Failed to decode unauthorized error: \(objcError.localizedDescription)"
  73. )
  74. }
  75. return StorageError.unauthorized(bucket, object)
  76. case FIRIMPLStorageErrorCode.retryLimitExceeded.rawValue: return StorageError.retryLimitExceeded
  77. case FIRIMPLStorageErrorCode.nonMatchingChecksum.rawValue: return StorageError
  78. .nonMatchingChecksum
  79. case FIRIMPLStorageErrorCode.downloadSizeExceeded.rawValue:
  80. guard let total = userInfo["totalSize"] as? Int64,
  81. let maxSize = userInfo["maxAllowedSize"] as? Int64 else {
  82. return StorageError
  83. .internalError(
  84. "Failed to decode downloadSizeExceeded error: \(objcError.localizedDescription)"
  85. )
  86. }
  87. return StorageError.downloadSizeExceeded(total, maxSize)
  88. case FIRIMPLStorageErrorCode.cancelled.rawValue: return StorageError.cancelled
  89. case FIRIMPLStorageErrorCode.invalidArgument.rawValue: return StorageError
  90. .invalidArgument(objcError.localizedDescription)
  91. default: return StorageError.internalError("Internal error converting ObjC Error to Swift")
  92. }
  93. }
  94. }