RuntimeError.swift 3.4 KB

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