Skip to content

Commit

Permalink
Use ConnectError.unavailable when client is offline (#328)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rebello95 authored Dec 31, 2024
1 parent 0820008 commit 20c7871
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,18 @@ extension Code {
return .canceled
case URLError.badURL.rawValue:
return .invalidArgument
case URLError.timedOut.rawValue:
case URLError.timedOut.rawValue, NSURLErrorTimedOut:
return .deadlineExceeded
case URLError.unsupportedURL.rawValue:
return .unimplemented
case NSURLErrorCannotConnectToHost,
NSURLErrorCannotFindHost,
NSURLErrorDataNotAllowed,
NSURLErrorInternationalRoamingOff,
NSURLErrorNetworkConnectionLost,
NSURLErrorNotConnectedToInternet,
NSURLErrorSecureConnectionFailed:
return .unavailable
case ...100: // URLSession can return errors in this range
return .unknown
default:
Expand Down
16 changes: 9 additions & 7 deletions Libraries/ConnectNIO/Public/NIOHTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
case connected(channel: NIOCore.Channel, multiplexer: NIOHTTP2.HTTP2StreamMultiplexer)
}

private enum Error: Swift.Error {
case disconnected
}

/// Designated initializer for the client.
///
/// - parameter host: Target host (e.g., `https://connectrpc.com`).
Expand Down Expand Up @@ -137,11 +133,11 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
)
} else {
onResponse(.init(
code: .unknown,
code: .unavailable,
headers: [:],
message: nil,
trailers: [:],
error: Error.disconnected,
error: ConnectError.disconnected(),
tracingInfo: nil
))
}
Expand All @@ -165,7 +161,7 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
for: request.url, on: eventLoop, using: multiplexer, with: handler
)
} else {
responseCallbacks.receiveClose(.unknown, [:], Error.disconnected)
responseCallbacks.receiveClose(.unavailable, [:], ConnectError.disconnected())
}
}
return .init(
Expand Down Expand Up @@ -268,3 +264,9 @@ open class NIOHTTPClient: Connect.HTTPClientInterface, @unchecked Sendable {
try? self.loopGroup.syncShutdownGracefully()
}
}

private extension ConnectError {
static func disconnected() -> Self {
return .init(code: .unavailable, message: "client is not connected")
}
}

0 comments on commit 20c7871

Please sign in to comment.