StorageError.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /// The error domain for codes in the `StorageErrorCode` enum.
  16. public let StorageErrorDomain: String = "FIRStorageErrorDomain"
  17. /**
  18. * Adds wrappers for common Firebase Storage errors (including creating errors from GCS errors).
  19. * For more information on unwrapping GCS errors, see the GCS errors docs:
  20. * https://cloud.google.com/storage/docs/json_api/v1/status-codes
  21. * This is never publicly exposed to end developers (as they will simply see an NSError).
  22. */
  23. @objc(FIRStorageErrorCode) public enum StorageErrorCode: Int, Swift.Error {
  24. case unknown = -13000
  25. case objectNotFound = -13010
  26. case bucketNotFound = -13011
  27. case projectNotFound = -13012
  28. case quotaExceeded = -13013
  29. case unauthenticated = -13020
  30. case unauthorized = -13021
  31. case retryLimitExceeded = -13030
  32. case nonMatchingChecksum = -13031
  33. case downloadSizeExceeded = -13032
  34. case cancelled = -13040
  35. case invalidArgument = -13050
  36. case bucketMismatch = -13051
  37. case internalError = -13052
  38. case pathError = -13053
  39. /**
  40. * Creates a Firebase Storage error from a specific GCS error and StorageReference.
  41. * @param serverError Server error to wrap and return as a Firebase Storage error.
  42. * @param ref StorageReference which provides context about the request being made.
  43. * @return Returns a Firebase Storage error.
  44. */
  45. static func error(withServerError serverError: NSError, ref: StorageReference) -> NSError {
  46. var errorDictionary = serverError.userInfo
  47. errorDictionary["ResponseErrorDomain"] = serverError.domain
  48. errorDictionary["ResponseErrorCode"] = serverError.code
  49. errorDictionary["bucket"] = ref.path.bucket
  50. errorDictionary[NSUnderlyingErrorKey] = serverError
  51. if let object = ref.path.object {
  52. errorDictionary["object"] = object
  53. }
  54. if let data = (errorDictionary["data"] as? Data) {
  55. errorDictionary["ResponseBody"] = String(data: data, encoding: .utf8)
  56. }
  57. let storageError = switch serverError.code {
  58. case 400: StorageError.unknown(
  59. message: "Unknown 400 error from backend",
  60. serverError: errorDictionary
  61. )
  62. case 401: StorageError.unauthenticated(serverError: errorDictionary)
  63. case 402: StorageError.quotaExceeded(
  64. bucket: ref.path.bucket,
  65. serverError: errorDictionary
  66. )
  67. case 403: StorageError.unauthorized(
  68. bucket: ref.path.bucket,
  69. object: ref.path.object ?? "<object-entity-internal-error>",
  70. serverError: errorDictionary
  71. )
  72. case 404: StorageError.objectNotFound(
  73. object: ref.path.object ?? "<object-entity-internal-error>", serverError: errorDictionary
  74. )
  75. default: StorageError.unknown(
  76. message: "Unexpected \(serverError.code) code from backend", serverError: errorDictionary
  77. )
  78. }
  79. return storageError as NSError
  80. }
  81. /** Creates a Firebase Storage error from an invalid request.
  82. *
  83. * @param request The Data representation of the invalid user request.
  84. * @return Returns the corresponding Firebase Storage error.
  85. */
  86. static func error(withInvalidRequest request: Data?) -> NSError {
  87. var requestString: String
  88. if let request {
  89. requestString = String(data: request, encoding: .utf8) ?? "<unstringable data>"
  90. } else {
  91. requestString = "<nil request returned from server>"
  92. }
  93. let invalidDataString = "Invalid data returned from the server: \(requestString)"
  94. return StorageError.unknown(message: invalidDataString, serverError: [:]) as NSError
  95. }
  96. }
  97. /// Firebase Storage errors
  98. public enum StorageError: Error, CustomNSError {
  99. case unknown(message: String, serverError: [String: Any])
  100. case objectNotFound(object: String, serverError: [String: Any])
  101. case bucketNotFound(bucket: String)
  102. case projectNotFound(project: String)
  103. case quotaExceeded(bucket: String, serverError: [String: Any])
  104. case unauthenticated(serverError: [String: Any])
  105. case unauthorized(bucket: String, object: String, serverError: [String: Any])
  106. case retryLimitExceeded
  107. case nonMatchingChecksum
  108. case downloadSizeExceeded(total: Int64, maxSize: Int64)
  109. case cancelled
  110. case invalidArgument(message: String)
  111. case internalError(message: String)
  112. case bucketMismatch(message: String)
  113. case pathError(message: String)
  114. // MARK: - CustomNSError
  115. /// Default domain of the error.
  116. public static var errorDomain: String { return StorageErrorDomain }
  117. /// The error code within the given domain.
  118. public var errorCode: Int {
  119. switch self {
  120. case .unknown:
  121. return StorageErrorCode.unknown.rawValue
  122. case .objectNotFound:
  123. return StorageErrorCode.objectNotFound.rawValue
  124. case .bucketNotFound:
  125. return StorageErrorCode.bucketNotFound.rawValue
  126. case .projectNotFound:
  127. return StorageErrorCode.projectNotFound.rawValue
  128. case .quotaExceeded:
  129. return StorageErrorCode.quotaExceeded.rawValue
  130. case .unauthenticated:
  131. return StorageErrorCode.unauthenticated.rawValue
  132. case .unauthorized:
  133. return StorageErrorCode.unauthorized.rawValue
  134. case .retryLimitExceeded:
  135. return StorageErrorCode.retryLimitExceeded.rawValue
  136. case .nonMatchingChecksum:
  137. return StorageErrorCode.nonMatchingChecksum.rawValue
  138. case .downloadSizeExceeded:
  139. return StorageErrorCode.downloadSizeExceeded.rawValue
  140. case .cancelled:
  141. return StorageErrorCode.cancelled.rawValue
  142. case .invalidArgument:
  143. return StorageErrorCode.invalidArgument.rawValue
  144. case .internalError:
  145. return StorageErrorCode.internalError.rawValue
  146. case .bucketMismatch:
  147. return StorageErrorCode.bucketMismatch.rawValue
  148. case .pathError:
  149. return StorageErrorCode.pathError.rawValue
  150. }
  151. }
  152. /// The default user-info dictionary.
  153. public var errorUserInfo: [String: Any] {
  154. switch self {
  155. case let .unknown(message, serverError):
  156. var dictionary = serverError
  157. dictionary[NSLocalizedDescriptionKey] = message
  158. return dictionary
  159. case let .objectNotFound(object, serverError):
  160. var dictionary = serverError
  161. dictionary[NSLocalizedDescriptionKey] = "Object \(object) does not exist."
  162. return dictionary
  163. case let .bucketNotFound(bucket):
  164. return [NSLocalizedDescriptionKey: "Bucket \(bucket) does not exist."]
  165. case let .projectNotFound(project):
  166. return [NSLocalizedDescriptionKey: "Project \(project) does not exist."]
  167. case let .quotaExceeded(bucket, serverError):
  168. var dictionary = serverError
  169. dictionary[NSLocalizedDescriptionKey] =
  170. "Quota for bucket \(bucket) exceeded, please view quota on firebase.google.com."
  171. return dictionary
  172. case let .unauthenticated(serverError):
  173. var dictionary = serverError
  174. dictionary[NSLocalizedDescriptionKey] = "User is not authenticated, please " +
  175. "authenticate using Firebase Authentication and try again."
  176. return dictionary
  177. case let .unauthorized(bucket, object, serverError):
  178. var dictionary = serverError
  179. dictionary[NSLocalizedDescriptionKey] =
  180. "User does not have permission to access gs://\(bucket)/\(object)."
  181. return dictionary
  182. case .retryLimitExceeded:
  183. return [NSLocalizedDescriptionKey: "Max retry time for operation exceeded, please try again."]
  184. case .nonMatchingChecksum:
  185. // TODO: replace with actual checksum strings when we choose to implement.
  186. return [NSLocalizedDescriptionKey: "Uploaded/downloaded object TODO has checksum: TODO " +
  187. "which does not match server checksum: TODO. Please retry the upload/download."]
  188. case let .downloadSizeExceeded(total, maxSize):
  189. var dictionary: [String: Any] = ["totalSize": total, "maxAllowedSize": maxSize]
  190. dictionary[NSLocalizedDescriptionKey] = "Attempted to download object with size of " +
  191. "\(total) bytes, " +
  192. "which exceeds the maximum size of \(maxSize) bytes. " +
  193. "Consider raising the maximum download maxSize, or using StorageReference.write"
  194. return dictionary
  195. case .cancelled:
  196. return [NSLocalizedDescriptionKey: "User cancelled the upload/download."]
  197. case let .invalidArgument(message):
  198. return [NSLocalizedDescriptionKey: message]
  199. case let .internalError(message):
  200. return [NSLocalizedDescriptionKey: message]
  201. case let .bucketMismatch(message):
  202. return [NSLocalizedDescriptionKey: message]
  203. case let .pathError(message):
  204. return [NSLocalizedDescriptionKey: message]
  205. }
  206. }
  207. }