RuntimeError.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, @unchecked Sendable {
  21. private var storage: Storage
  22. // Ensures the underlying storage is unique.
  23. private mutating func ensureUniqueStorage() {
  24. if !isKnownUniquelyReferenced(&self.storage) {
  25. self.storage = self.storage.copy()
  26. }
  27. }
  28. /// The code indicating the domain of the error.
  29. public var code: Code {
  30. get { self.storage.code }
  31. set {
  32. self.ensureUniqueStorage()
  33. self.storage.code = newValue
  34. }
  35. }
  36. /// A message providing more details about the error which may include details specific to this
  37. /// instance of the error.
  38. public var message: String {
  39. get { self.storage.message }
  40. set {
  41. self.ensureUniqueStorage()
  42. self.storage.message = newValue
  43. }
  44. }
  45. /// The original error which led to this error being thrown.
  46. public var cause: Error? {
  47. get { self.storage.cause }
  48. set {
  49. self.ensureUniqueStorage()
  50. self.storage.cause = newValue
  51. }
  52. }
  53. /// Creates a new error.
  54. ///
  55. /// - Parameters:
  56. /// - code: The error code.
  57. /// - message: A description of the error.
  58. /// - cause: The original error which led to this error being thrown.
  59. public init(code: Code, message: String, cause: Error? = nil) {
  60. self.storage = Storage(code: code, message: message, cause: cause)
  61. }
  62. }
  63. extension RuntimeError: CustomStringConvertible {
  64. public var description: String {
  65. if let cause = self.cause {
  66. return "\(self.code): \"\(self.message)\" (cause: \"\(cause)\")"
  67. } else {
  68. return "\(self.code): \"\(self.message)\""
  69. }
  70. }
  71. }
  72. extension RuntimeError {
  73. private final class Storage: Hashable {
  74. var code: Code
  75. var message: String
  76. var cause: Error?
  77. init(code: Code, message: String, cause: Error?) {
  78. self.code = code
  79. self.message = message
  80. self.cause = cause
  81. }
  82. func copy() -> Storage {
  83. return Storage(code: self.code, message: self.message, cause: self.cause)
  84. }
  85. func hash(into hasher: inout Hasher) {
  86. hasher.combine(self.code)
  87. hasher.combine(self.message)
  88. }
  89. static func == (lhs: Storage, rhs: Storage) -> Bool {
  90. return lhs.code == rhs.code && lhs.message == rhs.message
  91. }
  92. }
  93. }
  94. extension RuntimeError {
  95. public struct Code: Hashable, Sendable {
  96. private enum Value {
  97. case invalidArgument
  98. case serverIsAlreadyRunning
  99. case serverIsStopped
  100. case failedToStartTransport
  101. case noTransportsConfigured
  102. case clientIsAlreadyRunning
  103. case clientIsStopped
  104. case transportError
  105. }
  106. private var value: Value
  107. private init(_ value: Value) {
  108. self.value = value
  109. }
  110. /// An argument was invalid.
  111. public static var invalidArgument: Self {
  112. Self(.invalidArgument)
  113. }
  114. /// At attempt to start the server was made but it is already running.
  115. public static var serverIsAlreadyRunning: Self {
  116. Self(.serverIsAlreadyRunning)
  117. }
  118. /// At attempt to start the server was made but it has already stopped.
  119. public static var serverIsStopped: Self {
  120. Self(.serverIsStopped)
  121. }
  122. /// The server couldn't be started because a transport failed to start.
  123. public static var failedToStartTransport: Self {
  124. Self(.failedToStartTransport)
  125. }
  126. /// The server couldn't be started because no transports were configured.
  127. public static var noTransportsConfigured: Self {
  128. Self(.noTransportsConfigured)
  129. }
  130. /// At attempt to start the client was made but it is already running.
  131. public static var clientIsAlreadyRunning: Self {
  132. Self(.clientIsAlreadyRunning)
  133. }
  134. /// At attempt to start the client was made but it has already stopped.
  135. public static var clientIsStopped: Self {
  136. Self(.clientIsStopped)
  137. }
  138. /// The transport threw an error whilst connected.
  139. public static var transportError: Self {
  140. Self(.transportError)
  141. }
  142. }
  143. }
  144. extension RuntimeError.Code: CustomStringConvertible {
  145. public var description: String {
  146. String(describing: self.value)
  147. }
  148. }