GRPCError.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 timeout used for the RPC.
  51. public var timeout: GRPCTimeout
  52. public init(_ timeout: GRPCTimeout) {
  53. self.timeout = timeout
  54. }
  55. public var description: String {
  56. return "RPC timed out (timeout=\(self.timeout.wireEncoding)) before completing"
  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. /// It was not possible to decode a base64 message (gRPC-Web only).
  100. public struct Base64DecodeError: GRPCErrorProtocol {
  101. public let description = "Base64 message decoding failed"
  102. public init() {
  103. }
  104. public func makeGRPCStatus() -> GRPCStatus {
  105. return GRPCStatus(code: .internalError, message: self.description)
  106. }
  107. }
  108. /// The compression mechanism used was not supported.
  109. public struct CompressionUnsupported: GRPCErrorProtocol {
  110. public let description = "The compression used is not supported"
  111. public init() {
  112. }
  113. public func makeGRPCStatus() -> GRPCStatus {
  114. return GRPCStatus(code: .unimplemented, message: self.description)
  115. }
  116. }
  117. /// Too many, or too few, messages were sent over the given stream.
  118. public struct StreamCardinalityViolation: GRPCErrorProtocol {
  119. /// The stream on which there was a cardinality violation.
  120. public var stream: GRPCStreamType
  121. public init(stream: GRPCStreamType) {
  122. self.stream = stream
  123. }
  124. public var description: String {
  125. switch self.stream {
  126. case .request:
  127. return "Request stream cardinality violation"
  128. case .response:
  129. return "Response stream cardinality violation"
  130. }
  131. }
  132. public func makeGRPCStatus() -> GRPCStatus {
  133. return GRPCStatus(code: .internalError, message: self.description)
  134. }
  135. }
  136. /// The 'content-type' HTTP/2 header was missing or not valid.
  137. public struct InvalidContentType: GRPCErrorProtocol {
  138. /// The value of the 'content-type' header, if it was present.
  139. public var contentType: String?
  140. public init(_ contentType: String?) {
  141. self.contentType = contentType
  142. }
  143. public var description: String {
  144. if let contentType = self.contentType {
  145. return "Invalid 'content-type' header: '\(contentType)'"
  146. } else {
  147. return "Missing 'content-type' header"
  148. }
  149. }
  150. public func makeGRPCStatus() -> GRPCStatus {
  151. return GRPCStatus(code: .internalError, message: self.description)
  152. }
  153. }
  154. /// The ':status' HTTP/2 header was not "200".
  155. public struct InvalidHTTPStatus: GRPCErrorProtocol {
  156. /// The HTTP/2 ':status' header, if it was present.
  157. public var status: String?
  158. public init(_ status: String?) {
  159. self.status = status
  160. }
  161. public var description: String {
  162. if let status = status {
  163. return "Invalid HTTP response status: \(status)"
  164. } else {
  165. return "Missing HTTP ':status' header"
  166. }
  167. }
  168. public func makeGRPCStatus() -> GRPCStatus {
  169. return GRPCStatus(code: .init(httpStatus: self.status), message: self.description)
  170. }
  171. }
  172. /// The ':status' HTTP/2 header was not "200" but the 'grpc-status' header was present and valid.
  173. public struct InvalidHTTPStatusWithGRPCStatus: GRPCErrorProtocol {
  174. public var status: GRPCStatus
  175. public init(_ status: GRPCStatus) {
  176. self.status = status
  177. }
  178. public var description: String {
  179. return "Invalid HTTP response status, but gRPC status was present"
  180. }
  181. public func makeGRPCStatus() -> GRPCStatus {
  182. return self.status
  183. }
  184. }
  185. /// An invalid state has been reached; something has gone very wrong.
  186. public struct InvalidState: GRPCErrorProtocol {
  187. public var message: String
  188. public init(_ message: String) {
  189. self.message = message
  190. }
  191. public var description: String {
  192. return self.message
  193. }
  194. public func makeGRPCStatus() -> GRPCStatus {
  195. return GRPCStatus(code: .internalError, message: "Invalid state: \(self.message)")
  196. }
  197. }
  198. }
  199. extension GRPCError {
  200. struct WithContext: Error {
  201. var error: GRPCStatusTransformable
  202. var file: StaticString
  203. var line: Int
  204. var function: StaticString
  205. init(
  206. _ error: GRPCStatusTransformable,
  207. file: StaticString = #file,
  208. line: Int = #line,
  209. function: StaticString = #function
  210. ) {
  211. self.error = error
  212. self.file = file
  213. self.line = line
  214. self.function = function
  215. }
  216. }
  217. }
  218. /// Requirements for `GRPCError` types.
  219. public protocol GRPCErrorProtocol: GRPCStatusTransformable, Equatable, CustomStringConvertible {}
  220. extension GRPCErrorProtocol {
  221. /// Creates a `GRPCError.WithContext` containing a `GRPCError` and the location of the call site.
  222. internal func captureContext(
  223. file: StaticString = #file,
  224. line: Int = #line,
  225. function: StaticString = #file
  226. ) -> GRPCError.WithContext {
  227. return GRPCError.WithContext(self, file: file, line: line, function: function)
  228. }
  229. }
  230. /// The type of stream. Messages are sent from the client to the server on the request stream, and
  231. /// from the server to the client on the response stream.
  232. public enum GRPCStreamType {
  233. case request
  234. case response
  235. }
  236. extension GRPCStatus.Code {
  237. /// The gRPC status code associated with the given HTTP status code. This should only be used if
  238. /// the RPC did not return a 'grpc-status' trailer.
  239. internal init(httpStatus: String?) {
  240. /// See: https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
  241. switch httpStatus {
  242. case "400":
  243. self = .internalError
  244. case "401":
  245. self = .unauthenticated
  246. case "403":
  247. self = .permissionDenied
  248. case "404":
  249. self = .unimplemented
  250. case "429", "502", "503", "504":
  251. self = .unavailable
  252. default:
  253. self = .unknown
  254. }
  255. }
  256. }