GRPCError.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. }
  44. public func makeGRPCStatus() -> GRPCStatus {
  45. return GRPCStatus(code: .cancelled, message: self.description)
  46. }
  47. }
  48. /// The RPC did not complete before the timeout.
  49. public struct RPCTimedOut: GRPCErrorProtocol {
  50. /// The time limit which was exceeded by the RPC.
  51. public var timeLimit: TimeLimit
  52. public init(_ timeLimit: TimeLimit) {
  53. self.timeLimit = timeLimit
  54. }
  55. public var description: String {
  56. return "RPC timed out before completing (\(self.timeLimit))"
  57. }
  58. public func makeGRPCStatus() -> GRPCStatus {
  59. return GRPCStatus(code: .deadlineExceeded, message: self.description)
  60. }
  61. }
  62. /// A message was not able to be serialized.
  63. public struct SerializationFailure: GRPCErrorProtocol {
  64. public let description = "Message serialization failed"
  65. public init() {
  66. }
  67. public func makeGRPCStatus() -> GRPCStatus {
  68. return GRPCStatus(code: .internalError, message: self.description)
  69. }
  70. }
  71. /// A message was not able to be deserialized.
  72. public struct DeserializationFailure: GRPCErrorProtocol {
  73. public let description = "Message deserialization failed"
  74. public init() {
  75. }
  76. public func makeGRPCStatus() -> GRPCStatus {
  77. return GRPCStatus(code: .internalError, message: self.description)
  78. }
  79. }
  80. /// It was not possible to compress or decompress a message with zlib.
  81. public struct ZlibCompressionFailure: GRPCErrorProtocol {
  82. var code: Int32
  83. var message: String?
  84. public init(code: Int32, message: String?) {
  85. self.code = code
  86. self.message = message
  87. }
  88. public var description: String {
  89. if let message = self.message {
  90. return "Zlib error: \(self.code) \(message)"
  91. } else {
  92. return "Zlib error: \(self.code)"
  93. }
  94. }
  95. public func makeGRPCStatus() -> GRPCStatus {
  96. return GRPCStatus(code: .internalError, message: self.description)
  97. }
  98. }
  99. /// The decompression limit was exceeded while decompressing a message.
  100. public struct DecompressionLimitExceeded: GRPCErrorProtocol {
  101. /// The size of the compressed payload whose decompressed size exceeded the decompression limit.
  102. public let compressedSize: Int
  103. public init(compressedSize: Int) {
  104. self.compressedSize = compressedSize
  105. }
  106. public var description: String {
  107. return "Decompression limit exceeded with \(self.compressedSize) compressed bytes"
  108. }
  109. public func makeGRPCStatus() -> GRPCStatus {
  110. return GRPCStatus(code: .resourceExhausted, message: nil)
  111. }
  112. }
  113. /// It was not possible to decode a base64 message (gRPC-Web only).
  114. public struct Base64DecodeError: GRPCErrorProtocol {
  115. public let description = "Base64 message decoding failed"
  116. public init() {
  117. }
  118. public func makeGRPCStatus() -> GRPCStatus {
  119. return GRPCStatus(code: .internalError, message: self.description)
  120. }
  121. }
  122. /// The compression mechanism used was not supported.
  123. public struct CompressionUnsupported: GRPCErrorProtocol {
  124. public let description = "The compression used is not supported"
  125. public init() {
  126. }
  127. public func makeGRPCStatus() -> GRPCStatus {
  128. return GRPCStatus(code: .unimplemented, message: self.description)
  129. }
  130. }
  131. /// Too many, or too few, messages were sent over the given stream.
  132. public struct StreamCardinalityViolation: GRPCErrorProtocol {
  133. /// The stream on which there was a cardinality violation.
  134. public var stream: GRPCStreamType
  135. public init(stream: GRPCStreamType) {
  136. self.stream = stream
  137. }
  138. public var description: String {
  139. switch self.stream {
  140. case .request:
  141. return "Request stream cardinality violation"
  142. case .response:
  143. return "Response stream cardinality violation"
  144. }
  145. }
  146. public func makeGRPCStatus() -> GRPCStatus {
  147. return GRPCStatus(code: .internalError, message: self.description)
  148. }
  149. }
  150. /// The 'content-type' HTTP/2 header was missing or not valid.
  151. public struct InvalidContentType: GRPCErrorProtocol {
  152. /// The value of the 'content-type' header, if it was present.
  153. public var contentType: String?
  154. public init(_ contentType: String?) {
  155. self.contentType = contentType
  156. }
  157. public var description: String {
  158. if let contentType = self.contentType {
  159. return "Invalid 'content-type' header: '\(contentType)'"
  160. } else {
  161. return "Missing 'content-type' header"
  162. }
  163. }
  164. public func makeGRPCStatus() -> GRPCStatus {
  165. return GRPCStatus(code: .internalError, message: self.description)
  166. }
  167. }
  168. /// The ':status' HTTP/2 header was not "200".
  169. public struct InvalidHTTPStatus: GRPCErrorProtocol {
  170. /// The HTTP/2 ':status' header, if it was present.
  171. public var status: String?
  172. public init(_ status: String?) {
  173. self.status = status
  174. }
  175. public var description: String {
  176. if let status = status {
  177. return "Invalid HTTP response status: \(status)"
  178. } else {
  179. return "Missing HTTP ':status' header"
  180. }
  181. }
  182. public func makeGRPCStatus() -> GRPCStatus {
  183. return GRPCStatus(code: .init(httpStatus: self.status), message: self.description)
  184. }
  185. }
  186. /// The ':status' HTTP/2 header was not "200" but the 'grpc-status' header was present and valid.
  187. public struct InvalidHTTPStatusWithGRPCStatus: GRPCErrorProtocol {
  188. public var status: GRPCStatus
  189. public init(_ status: GRPCStatus) {
  190. self.status = status
  191. }
  192. public var description: String {
  193. return "Invalid HTTP response status, but gRPC status was present"
  194. }
  195. public func makeGRPCStatus() -> GRPCStatus {
  196. return self.status
  197. }
  198. }
  199. /// An invalid state has been reached; something has gone very wrong.
  200. public struct InvalidState: GRPCErrorProtocol {
  201. public var message: String
  202. public init(_ message: String) {
  203. self.message = message
  204. }
  205. public var description: String {
  206. return self.message
  207. }
  208. public func makeGRPCStatus() -> GRPCStatus {
  209. return GRPCStatus(code: .internalError, message: "Invalid state: \(self.message)")
  210. }
  211. }
  212. }
  213. extension GRPCError {
  214. struct WithContext: Error {
  215. var error: GRPCStatusTransformable
  216. var file: StaticString
  217. var line: Int
  218. var function: StaticString
  219. init(
  220. _ error: GRPCStatusTransformable,
  221. file: StaticString = #file,
  222. line: Int = #line,
  223. function: StaticString = #function
  224. ) {
  225. self.error = error
  226. self.file = file
  227. self.line = line
  228. self.function = function
  229. }
  230. }
  231. }
  232. /// Requirements for `GRPCError` types.
  233. public protocol GRPCErrorProtocol: GRPCStatusTransformable, Equatable, CustomStringConvertible {}
  234. extension GRPCErrorProtocol {
  235. /// Creates a `GRPCError.WithContext` containing a `GRPCError` and the location of the call site.
  236. internal func captureContext(
  237. file: StaticString = #file,
  238. line: Int = #line,
  239. function: StaticString = #function
  240. ) -> GRPCError.WithContext {
  241. return GRPCError.WithContext(self, file: file, line: line, function: function)
  242. }
  243. }
  244. /// The type of stream. Messages are sent from the client to the server on the request stream, and
  245. /// from the server to the client on the response stream.
  246. public enum GRPCStreamType {
  247. case request
  248. case response
  249. }
  250. extension GRPCStatus.Code {
  251. /// The gRPC status code associated with the given HTTP status code. This should only be used if
  252. /// the RPC did not return a 'grpc-status' trailer.
  253. internal init(httpStatus: String?) {
  254. /// See: https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
  255. switch httpStatus {
  256. case "400":
  257. self = .internalError
  258. case "401":
  259. self = .unauthenticated
  260. case "403":
  261. self = .permissionDenied
  262. case "404":
  263. self = .unimplemented
  264. case "429", "502", "503", "504":
  265. self = .unavailable
  266. default:
  267. self = .unknown
  268. }
  269. }
  270. }