Skip to content

Commit 6e94db5

Browse files
authored
Don't throw on empty responses (#28)
Motivation: The AsyncDNSResolver currently throws an error when there are no records associated with a name. However, most query responses are arrays or contain arrays. As such it's unclear to callers whether they should be catching an error of a particular type or checking for the presence of values some combination of the two. Modifications: - Modify the `DNSResolver` API to return `nil` where only a single value is expected (e.g. CNAME). - Update documentation on `DNSResolver` to clarify the expected return values when no values are resolved. - Update the c-ares backend to check for no data when parsing and return empty responses - Update the DNSSD backend to pass in `nil` data to the parser when the status is timeout - Remove the `noData` error code as it shouldn't be reachable anymore Result: Queries with no response either return empty or nil
1 parent b7079b7 commit 6e94db5

File tree

8 files changed

+239
-175
lines changed

8 files changed

+239
-175
lines changed

Sources/AsyncDNSResolver/AsyncDNSResolver.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public struct AsyncDNSResolver {
6464
}
6565

6666
/// See ``DNSResolver/queryCNAME(name:)``.
67-
public func queryCNAME(name: String) async throws -> String {
67+
public func queryCNAME(name: String) async throws -> String? {
6868
try await self.underlying.queryCNAME(name: name)
6969
}
7070

7171
/// See ``DNSResolver/querySOA(name:)``.
72-
public func querySOA(name: String) async throws -> SOARecord {
72+
public func querySOA(name: String) async throws -> SOARecord? {
7373
try await self.underlying.querySOA(name: name)
7474
}
7575

@@ -102,15 +102,15 @@ public protocol DNSResolver {
102102
/// - Parameters:
103103
/// - name: The name to resolve.
104104
///
105-
/// - Returns: ``ARecord``s for the given name.
105+
/// - Returns: ``ARecord``s for the given name, empty if no records were found.
106106
func queryA(name: String) async throws -> [ARecord]
107107

108108
/// Lookup AAAA records associated with `name`.
109109
///
110110
/// - Parameters:
111111
/// - name: The name to resolve.
112112
///
113-
/// - Returns: ``AAAARecord``s for the given name.
113+
/// - Returns: ``AAAARecord``s for the given name, empty if no records were found.
114114
func queryAAAA(name: String) async throws -> [AAAARecord]
115115

116116
/// Lookup NS record associated with `name`.
@@ -126,16 +126,16 @@ public protocol DNSResolver {
126126
/// - Parameters:
127127
/// - name: The name to resolve.
128128
///
129-
/// - Returns: CNAME for the given name.
130-
func queryCNAME(name: String) async throws -> String
129+
/// - Returns: CNAME for the given name, `nil` if no record was found.
130+
func queryCNAME(name: String) async throws -> String?
131131

132132
/// Lookup SOA record associated with `name`.
133133
///
134134
/// - Parameters:
135135
/// - name: The name to resolve.
136136
///
137-
/// - Returns: ``SOARecord`` for the given name.
138-
func querySOA(name: String) async throws -> SOARecord
137+
/// - Returns: ``SOARecord`` for the given name, `nil` if no record was found.
138+
func querySOA(name: String) async throws -> SOARecord?
139139

140140
/// Lookup PTR record associated with `name`.
141141
///
@@ -150,23 +150,23 @@ public protocol DNSResolver {
150150
/// - Parameters:
151151
/// - name: The name to resolve.
152152
///
153-
/// - Returns: ``MXRecord``s for the given name.
153+
/// - Returns: ``MXRecord``s for the given name, empty if no records were found.
154154
func queryMX(name: String) async throws -> [MXRecord]
155155

156156
/// Lookup TXT records associated with `name`.
157157
///
158158
/// - Parameters:
159159
/// - name: The name to resolve.
160160
///
161-
/// - Returns: ``TXTRecord``s for the given name.
161+
/// - Returns: ``TXTRecord``s for the given name, empty if no records were found.
162162
func queryTXT(name: String) async throws -> [TXTRecord]
163163

164164
/// Lookup SRV records associated with `name`.
165165
///
166166
/// - Parameters:
167167
/// - name: The name to resolve.
168168
///
169-
/// - Returns: ``SRVRecord``s for the given name.
169+
/// - Returns: ``SRVRecord``s for the given name, empty if no records were found.
170170
func querySRV(name: String) async throws -> [SRVRecord]
171171
}
172172

Sources/AsyncDNSResolver/Errors.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extension AsyncDNSResolver {
1818
public struct Error: Swift.Error, Hashable, CustomStringConvertible {
1919
public struct Code: Hashable, Sendable {
2020
fileprivate enum Value: Hashable, Sendable {
21-
case noData
2221
case invalidQuery
2322
case serverFailure
2423
case notFound
@@ -50,8 +49,6 @@ extension AsyncDNSResolver {
5049
self.value = value
5150
}
5251

53-
public static var noData: Self { Self(.noData) }
54-
5552
public static var invalidQuery: Self { Self(.invalidQuery) }
5653

5754
public static var serverFailure: Self { Self(.serverFailure) }
@@ -113,8 +110,6 @@ extension AsyncDNSResolver {
113110

114111
public var description: String {
115112
switch self.code.value {
116-
case .noData:
117-
return "no data: \(self.message)"
118113
case .invalidQuery:
119114
return "invalid query: \(self.message)"
120115
case .serverFailure:

0 commit comments

Comments
 (0)