Errors.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2023 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. struct RPCError: Error {
  16. let httpResponseCode: Int
  17. let message: String
  18. let status: RPCStatus
  19. let details: [ErrorDetails]
  20. private var errorInfo: ErrorDetails? {
  21. return details.first { $0.isErrorInfo() }
  22. }
  23. init(httpResponseCode: Int, message: String, status: RPCStatus, details: [ErrorDetails]) {
  24. self.httpResponseCode = httpResponseCode
  25. self.message = message
  26. self.status = status
  27. self.details = details
  28. }
  29. func isFirebaseMLServiceDisabledError() -> Bool {
  30. return details.contains { $0.isFirebaseMLServiceDisabledErrorDetails() }
  31. }
  32. }
  33. extension RPCError: Decodable {
  34. enum CodingKeys: CodingKey {
  35. case error
  36. }
  37. init(from decoder: Decoder) throws {
  38. let container = try decoder.container(keyedBy: CodingKeys.self)
  39. let status = try container.decode(ErrorStatus.self, forKey: .error)
  40. if let code = status.code {
  41. httpResponseCode = code
  42. } else {
  43. httpResponseCode = -1
  44. }
  45. if let message = status.message {
  46. self.message = message
  47. } else {
  48. message = "Unknown error."
  49. }
  50. if let rpcStatus = status.status {
  51. self.status = rpcStatus
  52. } else {
  53. self.status = .unknown
  54. }
  55. details = status.details
  56. }
  57. }
  58. struct ErrorStatus {
  59. let code: Int?
  60. let message: String?
  61. let status: RPCStatus?
  62. let details: [ErrorDetails]
  63. }
  64. struct ErrorDetails {
  65. static let errorInfoType = "type.googleapis.com/google.rpc.ErrorInfo"
  66. let type: String
  67. let reason: String?
  68. let domain: String?
  69. let metadata: [String: String]?
  70. func isErrorInfo() -> Bool {
  71. return type == ErrorDetails.errorInfoType
  72. }
  73. func isFirebaseMLServiceDisabledErrorDetails() -> Bool {
  74. guard isErrorInfo() else {
  75. return false
  76. }
  77. guard reason == "SERVICE_DISABLED" else {
  78. return false
  79. }
  80. guard domain == "googleapis.com" else {
  81. return false
  82. }
  83. guard let metadata, metadata["service"] == "firebaseml.googleapis.com" else {
  84. return false
  85. }
  86. return true
  87. }
  88. }
  89. extension ErrorDetails: Decodable, Equatable {
  90. enum CodingKeys: String, CodingKey {
  91. case type = "@type"
  92. case reason
  93. case domain
  94. case metadata
  95. }
  96. }
  97. extension ErrorStatus: Decodable {
  98. enum CodingKeys: CodingKey {
  99. case code
  100. case message
  101. case status
  102. case details
  103. }
  104. init(from decoder: Decoder) throws {
  105. let container = try decoder.container(keyedBy: CodingKeys.self)
  106. code = try container.decodeIfPresent(Int.self, forKey: .code)
  107. message = try container.decodeIfPresent(String.self, forKey: .message)
  108. do {
  109. status = try container.decodeIfPresent(RPCStatus.self, forKey: .status)
  110. } catch {
  111. status = .unknown
  112. }
  113. if container.contains(.details) {
  114. details = try container.decode([ErrorDetails].self, forKey: .details)
  115. } else {
  116. details = []
  117. }
  118. }
  119. }
  120. enum RPCStatus: String, Decodable {
  121. // Not an error; returned on success.
  122. case ok = "OK"
  123. // The operation was cancelled, typically by the caller.
  124. case cancelled = "CANCELLED"
  125. // Unknown error.
  126. case unknown = "UNKNOWN"
  127. // The client specified an invalid argument.
  128. case invalidArgument = "INVALID_ARGUMENT"
  129. // The deadline expired before the operation could complete.
  130. case deadlineExceeded = "DEADLINE_EXCEEDED"
  131. // Some requested entity (e.g., file or directory) was not found.
  132. case notFound = "NOT_FOUND"
  133. // The entity that a client attempted to create (e.g., file or directory) already exists.
  134. case alreadyExists = "ALREADY_EXISTS"
  135. // The caller does not have permission to execute the specified operation.
  136. case permissionDenied = "PERMISSION_DENIED"
  137. // The request does not have valid authentication credentials for the operation.
  138. case unauthenticated = "UNAUTHENTICATED"
  139. // Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system
  140. // is out of space.
  141. case resourceExhausted = "RESOURCE_EXHAUSTED"
  142. // The operation was rejected because the system is not in a state required for the operation's
  143. // execution.
  144. case failedPrecondition = "FAILED_PRECONDITION"
  145. // The operation was aborted, typically due to a concurrency issue such as a sequencer check
  146. // failure or transaction abort.
  147. case aborted = "ABORTED"
  148. // The operation was attempted past the valid range.
  149. case outOfRange = "OUT_OF_RANGE"
  150. // The operation is not implemented or is not supported/enabled in this service.
  151. case unimplemented = "UNIMPLEMENTED"
  152. // Internal errors.
  153. case internalError = "INTERNAL"
  154. // The service is currently unavailable.
  155. case unavailable = "UNAVAILABLE"
  156. // Unrecoverable data loss or corruption.
  157. case dataLoss = "DATA_LOSS"
  158. }
  159. enum InvalidCandidateError: Error {
  160. case emptyContent(underlyingError: Error)
  161. case malformedContent(underlyingError: Error)
  162. }