ClientError.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 client.
  17. ///
  18. /// In contrast to ``RPCError``, the ``ClientError`` represents errors which happen at a scope
  19. /// wider than an individual RPC. For example, attempting to start a client which is already
  20. /// stopped would result in a ``ClientError``.
  21. public struct ClientError: 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 ClientError: 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 ClientError {
  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 ClientError {
  96. public struct Code: Hashable, Sendable {
  97. private enum Value {
  98. case clientIsAlreadyRunning
  99. case clientIsStopped
  100. case transportError
  101. }
  102. private var value: Value
  103. private init(_ value: Value) {
  104. self.value = value
  105. }
  106. /// At attempt to start the client was made but it is already running.
  107. public static var clientIsAlreadyRunning: Self {
  108. Self(.clientIsAlreadyRunning)
  109. }
  110. /// At attempt to start the client was made but it has already stopped.
  111. public static var clientIsStopped: Self {
  112. Self(.clientIsStopped)
  113. }
  114. /// The transport threw an error whilst connected.
  115. public static var transportError: Self {
  116. Self(.transportError)
  117. }
  118. }
  119. }
  120. extension ClientError.Code: CustomStringConvertible {
  121. public var description: String {
  122. String(describing: self.value)
  123. }
  124. }