Errors.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 isVertexAIInFirebaseServiceDisabledError() -> Bool {
  30. return details.contains { $0.isVertexAIInFirebaseServiceDisabledErrorDetails() }
  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 isServiceDisabledError() -> Bool {
  74. return isErrorInfo() && reason == "SERVICE_DISABLED" && domain == "googleapis.com"
  75. }
  76. func isVertexAIInFirebaseServiceDisabledErrorDetails() -> Bool {
  77. guard isServiceDisabledError() else {
  78. return false
  79. }
  80. guard let metadata, metadata["service"] == "firebasevertexai.googleapis.com" else {
  81. return false
  82. }
  83. return true
  84. }
  85. }
  86. extension ErrorDetails: Decodable, Equatable {
  87. enum CodingKeys: String, CodingKey {
  88. case type = "@type"
  89. case reason
  90. case domain
  91. case metadata
  92. }
  93. }
  94. extension ErrorStatus: Decodable {
  95. enum CodingKeys: CodingKey {
  96. case code
  97. case message
  98. case status
  99. case details
  100. }
  101. init(from decoder: Decoder) throws {
  102. let container = try decoder.container(keyedBy: CodingKeys.self)
  103. code = try container.decodeIfPresent(Int.self, forKey: .code)
  104. message = try container.decodeIfPresent(String.self, forKey: .message)
  105. do {
  106. status = try container.decodeIfPresent(RPCStatus.self, forKey: .status)
  107. } catch {
  108. status = .unknown
  109. }
  110. if container.contains(.details) {
  111. details = try container.decode([ErrorDetails].self, forKey: .details)
  112. } else {
  113. details = []
  114. }
  115. }
  116. }
  117. enum RPCStatus: String, Decodable {
  118. // Not an error; returned on success.
  119. case ok = "OK"
  120. // The operation was cancelled, typically by the caller.
  121. case cancelled = "CANCELLED"
  122. // Unknown error.
  123. case unknown = "UNKNOWN"
  124. // The client specified an invalid argument.
  125. case invalidArgument = "INVALID_ARGUMENT"
  126. // The deadline expired before the operation could complete.
  127. case deadlineExceeded = "DEADLINE_EXCEEDED"
  128. // Some requested entity (e.g., file or directory) was not found.
  129. case notFound = "NOT_FOUND"
  130. // The entity that a client attempted to create (e.g., file or directory) already exists.
  131. case alreadyExists = "ALREADY_EXISTS"
  132. // The caller does not have permission to execute the specified operation.
  133. case permissionDenied = "PERMISSION_DENIED"
  134. // The request does not have valid authentication credentials for the operation.
  135. case unauthenticated = "UNAUTHENTICATED"
  136. // Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system
  137. // is out of space.
  138. case resourceExhausted = "RESOURCE_EXHAUSTED"
  139. // The operation was rejected because the system is not in a state required for the operation's
  140. // execution.
  141. case failedPrecondition = "FAILED_PRECONDITION"
  142. // The operation was aborted, typically due to a concurrency issue such as a sequencer check
  143. // failure or transaction abort.
  144. case aborted = "ABORTED"
  145. // The operation was attempted past the valid range.
  146. case outOfRange = "OUT_OF_RANGE"
  147. // The operation is not implemented or is not supported/enabled in this service.
  148. case unimplemented = "UNIMPLEMENTED"
  149. // Internal errors.
  150. case internalError = "INTERNAL"
  151. // The service is currently unavailable.
  152. case unavailable = "UNAVAILABLE"
  153. // Unrecoverable data loss or corruption.
  154. case dataLoss = "DATA_LOSS"
  155. }
  156. enum InvalidCandidateError: Error {
  157. case emptyContent(underlyingError: Error)
  158. case malformedContent(underlyingError: Error)
  159. }