From acda3cf69c95970e7e06b0f8ee654697c317c586 Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Mon, 23 Dec 2024 09:32:15 -0800 Subject: [PATCH 1/3] Use `unavailable` `ConnectError` when URLSession is offline Resolves https://github.com/connectrpc/connect-swift/issues/325. Signed-off-by: Michael Rebello --- .../Implementation/Clients/URLSessionHTTPClient.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift b/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift index f045732d..f535d60a 100644 --- a/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift +++ b/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift @@ -207,6 +207,15 @@ extension Code { return .deadlineExceeded case URLError.unsupportedURL.rawValue: return .unimplemented + case NSURLErrorCannotConnectToHost, + NSURLErrorCannotFindHost, + NSURLErrorDataNotAllowed, + NSURLErrorInternationalRoamingOff, + NSURLErrorNetworkConnectionLost, + NSURLErrorNotConnectedToInternet, + NSURLErrorSecureConnectionFailed, + NSURLErrorTimedOut: + return .unavailable case ...100: // URLSession can return errors in this range return .unknown default: From 239fc51e77a9b12f113da4d2387402d085789d42 Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Mon, 23 Dec 2024 11:17:46 -0800 Subject: [PATCH 2/3] fixup Signed-off-by: Michael Rebello --- .../Public/Implementation/Clients/URLSessionHTTPClient.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift b/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift index f535d60a..3d4ef113 100644 --- a/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift +++ b/Libraries/Connect/Public/Implementation/Clients/URLSessionHTTPClient.swift @@ -203,7 +203,7 @@ 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 @@ -213,8 +213,7 @@ extension Code { NSURLErrorInternationalRoamingOff, NSURLErrorNetworkConnectionLost, NSURLErrorNotConnectedToInternet, - NSURLErrorSecureConnectionFailed, - NSURLErrorTimedOut: + NSURLErrorSecureConnectionFailed: return .unavailable case ...100: // URLSession can return errors in this range return .unknown From 2e7b26dd6b50e8d4ac7eb240124b7fe047b9fa73 Mon Sep 17 00:00:00 2001 From: Michael Rebello Date: Fri, 27 Dec 2024 09:34:25 -0800 Subject: [PATCH 3/3] nio support Signed-off-by: Michael Rebello --- Libraries/ConnectNIO/Public/NIOHTTPClient.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Libraries/ConnectNIO/Public/NIOHTTPClient.swift b/Libraries/ConnectNIO/Public/NIOHTTPClient.swift index bdf88e9a..8825e83f 100644 --- a/Libraries/ConnectNIO/Public/NIOHTTPClient.swift +++ b/Libraries/ConnectNIO/Public/NIOHTTPClient.swift @@ -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`). @@ -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 )) } @@ -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( @@ -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") + } +}