RuntimeError.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2024, 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 at runtime.
  17. ///
  18. /// In contrast to ``RPCError``, the ``RuntimeError`` represents errors which happen at a scope
  19. /// wider than an individual RPC. For example, passing invalid configuration values.
  20. @available(gRPCSwift 2.0, *)
  21. public struct RuntimeError: Error, Hashable, Sendable {
  22. /// The code indicating the domain of the error.
  23. public var code: Code
  24. /// A message providing more details about the error which may include details specific to this
  25. /// instance of the error.
  26. public var message: String
  27. /// The original error which led to this error being thrown.
  28. public var cause: (any Error)?
  29. /// Creates a new error.
  30. ///
  31. /// - Parameters:
  32. /// - code: The error code.
  33. /// - message: A description of the error.
  34. /// - cause: The original error which led to this error being thrown.
  35. public init(code: Code, message: String, cause: (any Error)? = nil) {
  36. self.code = code
  37. self.message = message
  38. self.cause = cause
  39. }
  40. public func hash(into hasher: inout Hasher) {
  41. hasher.combine(self.code)
  42. hasher.combine(self.message)
  43. }
  44. public static func == (lhs: Self, rhs: Self) -> Bool {
  45. return lhs.code == rhs.code && lhs.message == rhs.message
  46. }
  47. }
  48. @available(gRPCSwift 2.0, *)
  49. extension RuntimeError: CustomStringConvertible {
  50. public var description: String {
  51. if let cause = self.cause {
  52. return "\(self.code): \"\(self.message)\" (cause: \"\(cause)\")"
  53. } else {
  54. return "\(self.code): \"\(self.message)\""
  55. }
  56. }
  57. }
  58. @available(gRPCSwift 2.0, *)
  59. extension RuntimeError {
  60. public struct Code: Hashable, Sendable {
  61. private enum Value {
  62. case invalidArgument
  63. case serverIsAlreadyRunning
  64. case serverIsStopped
  65. case clientIsAlreadyRunning
  66. case clientIsStopped
  67. case transportError
  68. }
  69. private var value: Value
  70. private init(_ value: Value) {
  71. self.value = value
  72. }
  73. /// An argument was invalid.
  74. public static var invalidArgument: Self {
  75. Self(.invalidArgument)
  76. }
  77. /// At attempt to start the server was made but it is already running.
  78. public static var serverIsAlreadyRunning: Self {
  79. Self(.serverIsAlreadyRunning)
  80. }
  81. /// At attempt to start the server was made but it has already stopped.
  82. public static var serverIsStopped: Self {
  83. Self(.serverIsStopped)
  84. }
  85. /// At attempt to start the client was made but it is already running.
  86. public static var clientIsAlreadyRunning: Self {
  87. Self(.clientIsAlreadyRunning)
  88. }
  89. /// At attempt to start the client was made but it has already stopped.
  90. public static var clientIsStopped: Self {
  91. Self(.clientIsStopped)
  92. }
  93. /// The transport threw an error whilst connected.
  94. public static var transportError: Self {
  95. Self(.transportError)
  96. }
  97. }
  98. }
  99. @available(gRPCSwift 2.0, *)
  100. extension RuntimeError.Code: CustomStringConvertible {
  101. public var description: String {
  102. String(describing: self.value)
  103. }
  104. }