StorageError.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 FirebaseStorageObjC
  16. public enum StorageError: Error {
  17. case unknown
  18. case objectNotFound(String)
  19. case bucketNotFound(String)
  20. case projectNotFound(String)
  21. case quotaExceeded(String)
  22. case unauthenticated
  23. case unauthorized(String, String)
  24. case retryLimitExceeded
  25. case nonMatchingChecksum
  26. case downloadSizeExceeded(Int64, Int64)
  27. case cancelled
  28. case invalidArgument(String)
  29. case internalError(String)
  30. static func swiftConvert(objcError: NSError) -> StorageError {
  31. let userInfo = objcError.userInfo
  32. switch objcError.code {
  33. case FIRIMPLStorageErrorCode.unknown.rawValue: return StorageError.unknown
  34. case FIRIMPLStorageErrorCode.objectNotFound.rawValue:
  35. guard let object = userInfo["object"] as? String else {
  36. return StorageError
  37. .internalError(
  38. "Failed to decode object not found error: \(objcError.localizedDescription)"
  39. )
  40. }
  41. return StorageError.objectNotFound(object)
  42. case FIRIMPLStorageErrorCode.bucketNotFound.rawValue:
  43. guard let bucket = userInfo["bucket"] as? String else {
  44. return StorageError
  45. .internalError(
  46. "Failed to decode bucket not found error: \(objcError.localizedDescription)"
  47. )
  48. }
  49. return StorageError.bucketNotFound(bucket)
  50. case FIRIMPLStorageErrorCode.projectNotFound.rawValue:
  51. guard let project = userInfo["project"] as? String else {
  52. return StorageError
  53. .internalError(
  54. "Failed to decode project not found error: \(objcError.localizedDescription)"
  55. )
  56. }
  57. return StorageError.projectNotFound(project)
  58. case FIRIMPLStorageErrorCode.quotaExceeded.rawValue:
  59. guard let bucket = userInfo["bucket"] as? String else {
  60. return StorageError
  61. .internalError("Failed to decode quota exceeded error: \(objcError.localizedDescription)")
  62. }
  63. return StorageError.quotaExceeded(bucket)
  64. case FIRIMPLStorageErrorCode.unauthenticated.rawValue: return StorageError.unauthenticated
  65. case FIRIMPLStorageErrorCode.unauthorized.rawValue:
  66. guard let bucket = userInfo["bucket"] as? String,
  67. let object = userInfo["object"] as? String else {
  68. return StorageError
  69. .internalError(
  70. "Failed to decode unauthorized error: \(objcError.localizedDescription)"
  71. )
  72. }
  73. return StorageError.unauthorized(bucket, object)
  74. case FIRIMPLStorageErrorCode.retryLimitExceeded.rawValue: return StorageError.retryLimitExceeded
  75. case FIRIMPLStorageErrorCode.nonMatchingChecksum.rawValue: return StorageError
  76. .nonMatchingChecksum
  77. case FIRIMPLStorageErrorCode.downloadSizeExceeded.rawValue:
  78. guard let total = userInfo["totalSize"] as? Int64,
  79. let maxSize = userInfo["maxAllowedSize"] as? Int64 else {
  80. return StorageError
  81. .internalError(
  82. "Failed to decode downloadSizeExceeded error: \(objcError.localizedDescription)"
  83. )
  84. }
  85. return StorageError.downloadSizeExceeded(total, maxSize)
  86. case FIRIMPLStorageErrorCode.cancelled.rawValue: return StorageError.cancelled
  87. case FIRIMPLStorageErrorCode.invalidArgument.rawValue: return StorageError
  88. .invalidArgument(objcError.localizedDescription)
  89. default: return StorageError.internalError("Internal error converting ObjC Error to Swift")
  90. }
  91. }
  92. }