|
15 | 15 | @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
16 | 16 | extension AsyncDNSResolver {
|
17 | 17 | /// Possible ``AsyncDNSResolver/AsyncDNSResolver`` errors.
|
18 |
| - public struct Error: Swift.Error, Hashable, CustomStringConvertible { |
| 18 | + public struct Error: Swift.Error, CustomStringConvertible { |
19 | 19 | public struct Code: Hashable, Sendable {
|
20 | 20 | fileprivate enum Value: Hashable, Sendable {
|
21 |
| - case invalidQuery |
22 |
| - case serverFailure |
23 |
| - case notFound |
24 |
| - case notImplemented |
25 |
| - case serverRefused |
26 | 21 | case badQuery
|
27 |
| - case badName |
28 |
| - case badFamily |
29 | 22 | case badResponse
|
30 | 23 | case connectionRefused
|
31 | 24 | case timeout
|
32 |
| - case eof |
33 |
| - case fileIO |
34 |
| - case noMemory |
35 |
| - case destruction |
36 |
| - case badString |
37 |
| - case badFlags |
38 |
| - case noName |
39 |
| - case badHints |
40 |
| - case notInitialized |
41 |
| - case initError |
42 |
| - case cancelled |
43 |
| - case service |
44 |
| - case other(Int) |
| 25 | + case internalError |
45 | 26 | }
|
46 | 27 |
|
47 | 28 | fileprivate var value: Value
|
48 | 29 | private init(_ value: Value) {
|
49 | 30 | self.value = value
|
50 | 31 | }
|
51 | 32 |
|
52 |
| - public static var invalidQuery: Self { Self(.invalidQuery) } |
53 |
| - |
54 |
| - public static var serverFailure: Self { Self(.serverFailure) } |
55 |
| - |
56 |
| - public static var notFound: Self { Self(.notFound) } |
57 |
| - |
58 |
| - public static var notImplemented: Self { Self(.notImplemented) } |
59 |
| - |
60 |
| - public static var serverRefused: Self { Self(.serverRefused) } |
61 |
| - |
| 33 | + /// The query was badly formed. |
62 | 34 | public static var badQuery: Self { Self(.badQuery) }
|
63 | 35 |
|
64 |
| - public static var badName: Self { Self(.badName) } |
65 |
| - |
66 |
| - public static var badFamily: Self { Self(.badFamily) } |
67 |
| - |
| 36 | + /// The response couldn't be parsed. |
68 | 37 | public static var badResponse: Self { Self(.badResponse) }
|
69 | 38 |
|
| 39 | + /// The server refused to accept a connection. |
70 | 40 | public static var connectionRefused: Self { Self(.connectionRefused) }
|
71 | 41 |
|
| 42 | + /// The query timed out. |
72 | 43 | public static var timeout: Self { Self(.timeout) }
|
73 | 44 |
|
74 |
| - public static var eof: Self { Self(.eof) } |
75 |
| - |
76 |
| - public static var fileIO: Self { Self(.fileIO) } |
77 |
| - |
78 |
| - public static var noMemory: Self { Self(.noMemory) } |
79 |
| - |
80 |
| - public static var destruction: Self { Self(.destruction) } |
81 |
| - |
82 |
| - public static var badString: Self { Self(.badString) } |
83 |
| - |
84 |
| - public static var badFlags: Self { Self(.badFlags) } |
85 |
| - |
86 |
| - public static var noName: Self { Self(.noName) } |
87 |
| - |
88 |
| - public static var badHints: Self { Self(.badHints) } |
89 |
| - |
90 |
| - public static var notInitialized: Self { Self(.notInitialized) } |
91 |
| - |
92 |
| - public static var initError: Self { Self(.initError) } |
93 |
| - |
94 |
| - public static var cancelled: Self { Self(.cancelled) } |
95 |
| - |
96 |
| - public static var service: Self { Self(.service) } |
97 |
| - |
98 |
| - public static func other(_ code: Int) -> Self { |
99 |
| - Self(.other(code)) |
100 |
| - } |
| 45 | + /// An internal error. |
| 46 | + public static var internalError: Self { Self(.internalError) } |
101 | 47 | }
|
102 | 48 |
|
103 | 49 | public var code: Code
|
104 | 50 | public var message: String
|
| 51 | + public var source: Swift.Error? |
105 | 52 |
|
106 |
| - public init(code: Code, message: String = "") { |
| 53 | + public init(code: Code, message: String = "", source: Swift.Error? = nil) { |
107 | 54 | self.code = code
|
108 | 55 | self.message = message
|
| 56 | + self.source = source |
109 | 57 | }
|
110 | 58 |
|
111 | 59 | public var description: String {
|
| 60 | + let name: String |
112 | 61 | switch self.code.value {
|
113 |
| - case .invalidQuery: |
114 |
| - return "invalid query: \(self.message)" |
115 |
| - case .serverFailure: |
116 |
| - return "server failure: \(self.message)" |
117 |
| - case .notFound: |
118 |
| - return "not found: \(self.message)" |
119 |
| - case .notImplemented: |
120 |
| - return "not implemented: \(self.message)" |
121 |
| - case .serverRefused: |
122 |
| - return "server refused: \(self.message)" |
123 | 62 | case .badQuery:
|
124 |
| - return "bad query: \(self.message)" |
125 |
| - case .badName: |
126 |
| - return "bad name: \(self.message)" |
127 |
| - case .badFamily: |
128 |
| - return "bad family: \(self.message)" |
| 63 | + name = "bad query" |
129 | 64 | case .badResponse:
|
130 |
| - return "bad response: \(self.message)" |
| 65 | + name = "bad response" |
131 | 66 | case .connectionRefused:
|
132 |
| - return "connection refused: \(self.message)" |
| 67 | + name = "connection refused" |
133 | 68 | case .timeout:
|
134 |
| - return "timeout: \(self.message)" |
135 |
| - case .eof: |
136 |
| - return "EOF: \(self.message)" |
137 |
| - case .fileIO: |
138 |
| - return "file IO: \(self.message)" |
139 |
| - case .noMemory: |
140 |
| - return "no memory: \(self.message)" |
141 |
| - case .destruction: |
142 |
| - return "destruction: \(self.message)" |
143 |
| - case .badString: |
144 |
| - return "bad string: \(self.message)" |
145 |
| - case .badFlags: |
146 |
| - return "bad flags: \(self.message)" |
147 |
| - case .noName: |
148 |
| - return "no name: \(self.message)" |
149 |
| - case .badHints: |
150 |
| - return "bad hints: \(self.message)" |
151 |
| - case .notInitialized: |
152 |
| - return "not initialized: \(self.message)" |
153 |
| - case .initError: |
154 |
| - return "initialization error: \(self.message)" |
155 |
| - case .cancelled: |
156 |
| - return "cancelled: \(self.message)" |
157 |
| - case .service: |
158 |
| - return "service: \(self.message)" |
159 |
| - case .other(let code): |
160 |
| - return "other [\(code)]: \(self.message)" |
| 69 | + name = "timeout" |
| 70 | + case .internalError: |
| 71 | + name = "internal" |
161 | 72 | }
|
| 73 | + |
| 74 | + let suffix = self.source.map { " (\($0))" } ?? "" |
| 75 | + return "\(name): \(self.message)\(suffix)" |
162 | 76 | }
|
163 | 77 | }
|
164 | 78 | }
|
| 79 | + |
| 80 | +/// An error thrown from c-ares. |
| 81 | +public struct CAresError: Error, Hashable, Sendable { |
| 82 | + /// The error code. |
| 83 | + public var code: Int |
| 84 | + |
| 85 | + public init(code: Int) { |
| 86 | + self.code = code |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// An error thrown from DNSSD. |
| 91 | +public struct DNSSDError: Error, Hashable, Sendable { |
| 92 | + /// The error code. |
| 93 | + public var code: Int |
| 94 | + |
| 95 | + public init(code: Int) { |
| 96 | + self.code = code |
| 97 | + } |
| 98 | +} |
0 commit comments