Errors.swift 4.1 KB

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