ServerError.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2023, 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. /// A runtime error thrown by the server.
  17. ///
  18. /// In contrast to ``RPCError``, the ``ServerError`` represents errors which happen at a scope
  19. /// wider than an individual RPC. For example, attempting to start a server which is already
  20. /// stopped would result in a ``ServerError``.
  21. public struct ServerError: Error, Hashable, @unchecked Sendable {
  22. private var storage: Storage
  23. // Ensures the underlying storage is unique.
  24. private mutating func ensureUniqueStorage() {
  25. if !isKnownUniquelyReferenced(&self.storage) {
  26. self.storage = self.storage.copy()
  27. }
  28. }
  29. /// The code indicating the domain of the error.
  30. public var code: Code {
  31. get { self.storage.code }
  32. set {
  33. self.ensureUniqueStorage()
  34. self.storage.code = newValue
  35. }
  36. }
  37. /// A message providing more details about the error which may include details specific to this
  38. /// instance of the error.
  39. public var message: String {
  40. get { self.storage.message }
  41. set {
  42. self.ensureUniqueStorage()
  43. self.storage.message = newValue
  44. }
  45. }
  46. /// The original error which led to this error being thrown.
  47. public var cause: Error? {
  48. get { self.storage.cause }
  49. set {
  50. self.ensureUniqueStorage()
  51. self.storage.cause = newValue
  52. }
  53. }
  54. /// Creates a new error.
  55. ///
  56. /// - Parameters:
  57. /// - code: The error code.
  58. /// - message: A description of the error.
  59. /// - cause: The original error which led to this error being thrown.
  60. public init(code: Code, message: String, cause: Error? = nil) {
  61. self.storage = Storage(code: code, message: message, cause: cause)
  62. }
  63. }
  64. extension ServerError: CustomStringConvertible {
  65. public var description: String {
  66. if let cause = self.cause {
  67. return "\(self.code): \"\(self.message)\" (cause: \"\(cause)\")"
  68. } else {
  69. return "\(self.code): \"\(self.message)\""
  70. }
  71. }
  72. }
  73. extension ServerError {
  74. private final class Storage: Hashable {
  75. var code: Code
  76. var message: String
  77. var cause: Error?
  78. init(code: Code, message: String, cause: Error?) {
  79. self.code = code
  80. self.message = message
  81. self.cause = cause
  82. }
  83. func copy() -> Storage {
  84. return Storage(code: self.code, message: self.message, cause: self.cause)
  85. }
  86. func hash(into hasher: inout Hasher) {
  87. hasher.combine(self.code)
  88. hasher.combine(self.message)
  89. }
  90. static func == (lhs: Storage, rhs: Storage) -> Bool {
  91. return lhs.code == rhs.code && lhs.message == rhs.message
  92. }
  93. }
  94. }
  95. extension ServerError {
  96. public struct Code: Hashable, Sendable {
  97. private enum Value {
  98. case serverIsAlreadyRunning
  99. case serverIsStopped
  100. case failedToStartTransport
  101. case noTransportsConfigured
  102. }
  103. private var value: Value
  104. private init(_ value: Value) {
  105. self.value = value
  106. }
  107. /// At attempt to start the server was made but it is already running.
  108. public static var serverIsAlreadyRunning: Self {
  109. Self(.serverIsAlreadyRunning)
  110. }
  111. /// At attempt to start the server was made but it has already stopped.
  112. public static var serverIsStopped: Self {
  113. Self(.serverIsStopped)
  114. }
  115. /// The server couldn't be started because a transport failed to start.
  116. public static var failedToStartTransport: Self {
  117. Self(.failedToStartTransport)
  118. }
  119. /// The server couldn't be started because no transports were configured.
  120. public static var noTransportsConfigured: Self {
  121. Self(.noTransportsConfigured)
  122. }
  123. }
  124. }
  125. extension ServerError.Code: CustomStringConvertible {
  126. public var description: String {
  127. String(describing: self.value)
  128. }
  129. }