GRPCServerError.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import Foundation
  17. public enum GRPCServerError: Error, Equatable {
  18. /// The RPC method is not implemented on the server.
  19. case unimplementedMethod(String)
  20. /// It was not possible to decode a base64 message (gRPC-Web only).
  21. case base64DecodeError
  22. /// It was not possible to parse the request protobuf.
  23. case requestProtoParseFailure
  24. /// It was not possible to serialize the response protobuf.
  25. case responseProtoSerializationFailure
  26. /// The given compression mechanism is not supported.
  27. case unsupportedCompressionMechanism(String)
  28. /// Compression was indicated in the gRPC message, but not for the call.
  29. case unexpectedCompression
  30. /// More than one request was sent for a unary-request call.
  31. case requestCardinalityViolation
  32. /// The server received a message when it was not in a writable state.
  33. case serverNotWritable
  34. /// An invalid state has been reached; something has gone very wrong.
  35. case invalidState(String)
  36. }
  37. extension GRPCServerError: GRPCStatusTransformable {
  38. public func asGRPCStatus() -> GRPCStatus {
  39. // These status codes are informed by: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
  40. switch self {
  41. case .unimplementedMethod(let method):
  42. return GRPCStatus(code: .unimplemented, message: "unknown method \(method)")
  43. case .base64DecodeError:
  44. return GRPCStatus(code: .internalError, message: "could not decode base64 message")
  45. case .requestProtoParseFailure:
  46. return GRPCStatus(code: .internalError, message: "could not parse request proto")
  47. case .responseProtoSerializationFailure:
  48. return GRPCStatus(code: .internalError, message: "could not serialize response proto")
  49. case .unsupportedCompressionMechanism(let mechanism):
  50. return GRPCStatus(code: .unimplemented, message: "unsupported compression mechanism \(mechanism)")
  51. case .unexpectedCompression:
  52. return GRPCStatus(code: .unimplemented, message: "compression was enabled for this gRPC message but not for this call")
  53. case .requestCardinalityViolation:
  54. return GRPCStatus(code: .unimplemented, message: "request cardinality violation; method requires exactly one request but client sent more")
  55. case .serverNotWritable, .invalidState:
  56. return GRPCStatus.processingError
  57. }
  58. }
  59. }