GRPCError.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /// An error thrown by the gRPC library.
  17. ///
  18. /// Implementation details: this is a case-less `enum` with an inner-class per error type. This
  19. /// allows for additional error classes to be added as a SemVer minor change.
  20. ///
  21. /// Unfortunately it is not possible to use a private inner `enum` with static property 'cases' on
  22. /// the outer type to mirror each case of the inner `enum` as many of the errors require associated
  23. /// values (pattern matching is not possible).
  24. public enum GRPCError {
  25. /// The RPC is not implemented on the server.
  26. public struct RPCNotImplemented: GRPCErrorProtocol {
  27. /// The path of the RPC which was called, e.g. '/echo.Echo/Get'.
  28. public var rpc: String
  29. public init(rpc: String) {
  30. self.rpc = rpc
  31. }
  32. public var description: String {
  33. return "RPC '\(self.rpc)' is not implemented"
  34. }
  35. public func makeGRPCStatus() -> GRPCStatus {
  36. return GRPCStatus(code: .unimplemented, message: self.description)
  37. }
  38. }
  39. /// The RPC was cancelled by the client.
  40. public struct RPCCancelledByClient: GRPCErrorProtocol {
  41. public let description: String = "RPC was cancelled by the client"
  42. public init() {}
  43. public func makeGRPCStatus() -> GRPCStatus {
  44. return GRPCStatus(code: .cancelled, message: self.description)
  45. }
  46. }
  47. /// The RPC did not complete before the timeout.
  48. public struct RPCTimedOut: GRPCErrorProtocol {
  49. /// The time limit which was exceeded by the RPC.
  50. public var timeLimit: TimeLimit
  51. public init(_ timeLimit: TimeLimit) {
  52. self.timeLimit = timeLimit
  53. }
  54. public var description: String {
  55. return "RPC timed out before completing"
  56. }
  57. public func makeGRPCStatus() -> GRPCStatus {
  58. return GRPCStatus(code: .deadlineExceeded, message: self.description)
  59. }
  60. }
  61. /// A message was not able to be serialized.
  62. public struct SerializationFailure: GRPCErrorProtocol {
  63. public let description = "Message serialization failed"
  64. public init() {}
  65. public func makeGRPCStatus() -> GRPCStatus {
  66. return GRPCStatus(code: .internalError, message: self.description)
  67. }
  68. }
  69. /// A message was not able to be deserialized.
  70. public struct DeserializationFailure: GRPCErrorProtocol {
  71. public let description = "Message deserialization failed"
  72. public init() {}
  73. public func makeGRPCStatus() -> GRPCStatus {
  74. return GRPCStatus(code: .internalError, message: self.description)
  75. }
  76. }
  77. /// The length of the received payload was longer than is permitted.
  78. public struct PayloadLengthLimitExceeded: GRPCErrorProtocol {
  79. public let description: String
  80. public init(actualLength length: Int, limit: Int) {
  81. self.description = "Payload length exceeds limit (\(length) > \(limit))"
  82. }
  83. public func makeGRPCStatus() -> GRPCStatus {
  84. return GRPCStatus(code: .resourceExhausted, message: self.description)
  85. }
  86. }
  87. /// It was not possible to compress or decompress a message with zlib.
  88. public struct ZlibCompressionFailure: GRPCErrorProtocol {
  89. var code: Int32
  90. var message: String?
  91. public init(code: Int32, message: String?) {
  92. self.code = code
  93. self.message = message
  94. }
  95. public var description: String {
  96. if let message = self.message {
  97. return "Zlib error: \(self.code) \(message)"
  98. } else {
  99. return "Zlib error: \(self.code)"
  100. }
  101. }
  102. public func makeGRPCStatus() -> GRPCStatus {
  103. return GRPCStatus(code: .internalError, message: self.description)
  104. }
  105. }
  106. /// The decompression limit was exceeded while decompressing a message.
  107. public struct DecompressionLimitExceeded: GRPCErrorProtocol {
  108. /// The size of the compressed payload whose decompressed size exceeded the decompression limit.
  109. public let compressedSize: Int
  110. public init(compressedSize: Int) {
  111. self.compressedSize = compressedSize
  112. }
  113. public var description: String {
  114. return "Decompression limit exceeded with \(self.compressedSize) compressed bytes"
  115. }
  116. public func makeGRPCStatus() -> GRPCStatus {
  117. return GRPCStatus(code: .resourceExhausted, message: nil)
  118. }
  119. }
  120. /// It was not possible to decode a base64 message (gRPC-Web only).
  121. public struct Base64DecodeError: GRPCErrorProtocol {
  122. public let description = "Base64 message decoding failed"
  123. public init() {}
  124. public func makeGRPCStatus() -> GRPCStatus {
  125. return GRPCStatus(code: .internalError, message: self.description)
  126. }
  127. }
  128. /// The compression mechanism used was not supported.
  129. public struct CompressionUnsupported: GRPCErrorProtocol {
  130. public let description = "The compression used is not supported"
  131. public init() {}
  132. public func makeGRPCStatus() -> GRPCStatus {
  133. return GRPCStatus(code: .unimplemented, message: self.description)
  134. }
  135. }
  136. /// Too many, or too few, messages were sent over the given stream.
  137. public struct StreamCardinalityViolation: GRPCErrorProtocol {
  138. /// The stream on which there was a cardinality violation.
  139. public let description: String
  140. /// A request stream cardinality violation.
  141. public static let request = StreamCardinalityViolation("Request stream cardinality violation")
  142. /// A response stream cardinality violation.
  143. public static let response = StreamCardinalityViolation("Response stream cardinality violation")
  144. private init(_ description: String) {
  145. self.description = description
  146. }
  147. public func makeGRPCStatus() -> GRPCStatus {
  148. return GRPCStatus(code: .internalError, message: self.description)
  149. }
  150. }
  151. /// The 'content-type' HTTP/2 header was missing or not valid.
  152. public struct InvalidContentType: GRPCErrorProtocol {
  153. /// The value of the 'content-type' header, if it was present.
  154. public var contentType: String?
  155. public init(_ contentType: String?) {
  156. self.contentType = contentType
  157. }
  158. public var description: String {
  159. if let contentType = self.contentType {
  160. return "Invalid 'content-type' header: '\(contentType)'"
  161. } else {
  162. return "Missing 'content-type' header"
  163. }
  164. }
  165. public func makeGRPCStatus() -> GRPCStatus {
  166. return GRPCStatus(code: .internalError, message: self.description)
  167. }
  168. }
  169. /// The ':status' HTTP/2 header was not "200".
  170. public struct InvalidHTTPStatus: GRPCErrorProtocol {
  171. /// The HTTP/2 ':status' header, if it was present.
  172. public var status: String?
  173. public init(_ status: String?) {
  174. self.status = status
  175. }
  176. public var description: String {
  177. if let status = status {
  178. return "Invalid HTTP response status: \(status)"
  179. } else {
  180. return "Missing HTTP ':status' header"
  181. }
  182. }
  183. public func makeGRPCStatus() -> GRPCStatus {
  184. return GRPCStatus(code: .init(httpStatus: self.status), message: self.description)
  185. }
  186. }
  187. /// The ':status' HTTP/2 header was not "200" but the 'grpc-status' header was present and valid.
  188. public struct InvalidHTTPStatusWithGRPCStatus: GRPCErrorProtocol {
  189. public var status: GRPCStatus
  190. public init(_ status: GRPCStatus) {
  191. self.status = status
  192. }
  193. public var description: String {
  194. return "Invalid HTTP response status, but gRPC status was present"
  195. }
  196. public func makeGRPCStatus() -> GRPCStatus {
  197. return self.status
  198. }
  199. }
  200. /// Action was taken after the RPC had already completed.
  201. public struct AlreadyComplete: GRPCErrorProtocol {
  202. public var description: String {
  203. return "The RPC has already completed"
  204. }
  205. public init() {}
  206. public func makeGRPCStatus() -> GRPCStatus {
  207. return GRPCStatus(code: .unavailable, message: self.description)
  208. }
  209. }
  210. /// An invalid state has been reached; something has gone very wrong.
  211. public struct InvalidState: GRPCErrorProtocol {
  212. public var message: String
  213. public init(_ message: String) {
  214. self.message = "Invalid state: \(message)"
  215. }
  216. public var description: String {
  217. return self.message
  218. }
  219. public func makeGRPCStatus() -> GRPCStatus {
  220. return GRPCStatus(code: .internalError, message: self.message)
  221. }
  222. }
  223. public struct ProtocolViolation: GRPCErrorProtocol {
  224. public var message: String
  225. public init(_ message: String) {
  226. self.message = "Protocol violation: \(message)"
  227. }
  228. public var description: String {
  229. return self.message
  230. }
  231. public func makeGRPCStatus() -> GRPCStatus {
  232. return GRPCStatus(code: .internalError, message: self.message)
  233. }
  234. }
  235. }
  236. extension GRPCError {
  237. struct WithContext: Error, GRPCStatusTransformable {
  238. var error: GRPCStatusTransformable
  239. var file: StaticString
  240. var line: Int
  241. var function: StaticString
  242. init(
  243. _ error: GRPCStatusTransformable,
  244. file: StaticString = #file,
  245. line: Int = #line,
  246. function: StaticString = #function
  247. ) {
  248. self.error = error
  249. self.file = file
  250. self.line = line
  251. self.function = function
  252. }
  253. func makeGRPCStatus() -> GRPCStatus {
  254. return self.error.makeGRPCStatus()
  255. }
  256. }
  257. }
  258. /// Requirements for `GRPCError` types.
  259. public protocol GRPCErrorProtocol: GRPCStatusTransformable, Equatable, CustomStringConvertible {}
  260. extension GRPCErrorProtocol {
  261. /// Creates a `GRPCError.WithContext` containing a `GRPCError` and the location of the call site.
  262. internal func captureContext(
  263. file: StaticString = #file,
  264. line: Int = #line,
  265. function: StaticString = #function
  266. ) -> GRPCError.WithContext {
  267. return GRPCError.WithContext(self, file: file, line: line, function: function)
  268. }
  269. }
  270. extension GRPCStatus.Code {
  271. /// The gRPC status code associated with the given HTTP status code. This should only be used if
  272. /// the RPC did not return a 'grpc-status' trailer.
  273. internal init(httpStatus: String?) {
  274. /// See: https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
  275. switch httpStatus {
  276. case "400":
  277. self = .internalError
  278. case "401":
  279. self = .unauthenticated
  280. case "403":
  281. self = .permissionDenied
  282. case "404":
  283. self = .unimplemented
  284. case "429", "502", "503", "504":
  285. self = .unavailable
  286. default:
  287. self = .unknown
  288. }
  289. }
  290. }