CodeGenError.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 error thrown by the ``SourceGenerator`` to signal errors in the ``CodeGenerationRequest`` object.
  17. @available(gRPCSwift 2.0, *)
  18. public struct CodeGenError: Error, Hashable, Sendable {
  19. /// The code indicating the domain of the error.
  20. public var code: Code
  21. /// A message providing more details about the error which may include details specific to this
  22. /// instance of the error.
  23. public var message: String
  24. /// Creates a new error.
  25. ///
  26. /// - Parameters:
  27. /// - code: The error code.
  28. /// - message: A description of the error.
  29. public init(code: Code, message: String) {
  30. self.code = code
  31. self.message = message
  32. }
  33. }
  34. @available(gRPCSwift 2.0, *)
  35. extension CodeGenError {
  36. public struct Code: Hashable, Sendable {
  37. private enum Value {
  38. case nonUniqueServiceName
  39. case nonUniqueMethodName
  40. case invalidKind
  41. }
  42. private var value: Value
  43. private init(_ value: Value) {
  44. self.value = value
  45. }
  46. /// The same name is used for two services that are either in the same namespace or don't have a namespace.
  47. public static var nonUniqueServiceName: Self {
  48. Self(.nonUniqueServiceName)
  49. }
  50. /// The same name is used for two methods of the same service.
  51. public static var nonUniqueMethodName: Self {
  52. Self(.nonUniqueMethodName)
  53. }
  54. /// An invalid kind name is used for an import.
  55. public static var invalidKind: Self {
  56. Self(.invalidKind)
  57. }
  58. }
  59. }
  60. @available(gRPCSwift 2.0, *)
  61. extension CodeGenError: CustomStringConvertible {
  62. public var description: String {
  63. return "\(self.code): \"\(self.message)\""
  64. }
  65. }