Skip to content

Commit 20c7871

Browse files
authored
Use ConnectError.unavailable when client is offline (#328)
Resolves #325. When turning the device's wifi off, the following was observed with URLSession: Before: ``` ▿ Optional<ConnectError> ▿ some : ConnectError - code : Connect.Code.unknown ▿ message : Optional<String> - some : "The Internet connection appears to be offline." ▿ exception : Optional<Error> - some : Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." ``` Based on the discussion in the issue linked above, this PR updates the code to be `.unavailable`: ``` ▿ Optional<ConnectError> ▿ some : ConnectError - code : Connect.Code.unavailable ▿ message : Optional<String> - some : "The Internet connection appears to be offline." ▿ exception : Optional<Error> - some : Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." ``` The NIO client has also been updated to return the `.unavailable` status code: ``` ▿ Optional<ConnectError> ▿ some : ConnectError - code : Connect.Code.unavailable ▿ message : Optional<String> - some : "client is not connected" - exception : nil ``` --------- Signed-off-by: Michael Rebello <me@michaelrebello.com>
1 parent 0820008 commit 20c7871

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,18 @@ extension Code {
203203
return .canceled
204204
case URLError.badURL.rawValue:
205205
return .invalidArgument
206-
case URLError.timedOut.rawValue:
206+
case URLError.timedOut.rawValue, NSURLErrorTimedOut:
207207
return .deadlineExceeded
208208
case URLError.unsupportedURL.rawValue:
209209
return .unimplemented
210+
case NSURLErrorCannotConnectToHost,
211+
NSURLErrorCannotFindHost,
212+
NSURLErrorDataNotAllowed,
213+
NSURLErrorInternationalRoamingOff,
214+
NSURLErrorNetworkConnectionLost,
215+
NSURLErrorNotConnectedToInternet,
216+
NSURLErrorSecureConnectionFailed:
217+
return .unavailable
210218
case ...100: // URLSession can return errors in this range
211219
return .unknown
212220
default:

Libraries/ConnectNIO/Public/NIOHTTPClient.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
4141
case connected(channel: NIOCore.Channel, multiplexer: NIOHTTP2.HTTP2StreamMultiplexer)
4242
}
4343

44-
private enum Error: Swift.Error {
45-
case disconnected
46-
}
47-
4844
/// Designated initializer for the client.
4945
///
5046
/// - parameter host: Target host (e.g., `https://connectrpc.com`).
@@ -137,11 +133,11 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
137133
)
138134
} else {
139135
onResponse(.init(
140-
code: .unknown,
136+
code: .unavailable,
141137
headers: [:],
142138
message: nil,
143139
trailers: [:],
144-
error: Error.disconnected,
140+
error: ConnectError.disconnected(),
145141
tracingInfo: nil
146142
))
147143
}
@@ -165,7 +161,7 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
165161
for: request.url, on: eventLoop, using: multiplexer, with: handler
166162
)
167163
} else {
168-
responseCallbacks.receiveClose(.unknown, [:], Error.disconnected)
164+
responseCallbacks.receiveClose(.unavailable, [:], ConnectError.disconnected())
169165
}
170166
}
171167
return .init(
@@ -268,3 +264,9 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
268264
try? self.loopGroup.syncShutdownGracefully()
269265
}
270266
}
267+
268+
private extension ConnectError {
269+
static func disconnected() -> Self {
270+
return .init(code: .unavailable, message: "client is not connected")
271+
}
272+
}

0 commit comments

Comments
 (0)