CodeGenError.swift 2.1 KB

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