Errors.swift 5.9 KB

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