Errors.swift 4.7 KB

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