GRPCError.swift 8.3 KB

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