Skip to content

HTTPClientResponseDelegate is not notified of some errors (connection related?). #242

Closed
@kuwerty

Description

@kuwerty

Tested against commit 8add6b8 on Ubuntu 16.04 with Swift version 5.2.4 (swift-5.2.4-RELEASE)

I've been debugging an issue where an HTTPClientResponseDelegate does not get notified of some errors via the didReceiveError(task:...) method.

My expectation was that HTTPClientResponseDelegate would see any error throughout the entire lifecycle of the request, not just some subset of them.

I've pasted a test case below that demonstrates the issue with remoteConnectionClosed errors but I've seen it with connectTimeout too.

I can appreciate that the error was returned in Task.futureResult but it took quite a bit of effort to work that out (no matter how obvious it is in retrospect). However if the current behaviour is intended then I would still push that the documentation for didReceiveError is updated to emphasize it only sees some subset of errors and not "any network-related error" as it states now.

Thank you!

    func testDelegateConnectionFailError() {
        class TestDelegate: HTTPClientResponseDelegate {
            typealias Response = Void

            var error : Error?

            func didFinishRequest(task: HTTPClient.Task<Response>) throws {}

            func didReceiveError(task: HTTPClient.Task<Response>, _ error: Error) -> EventLoopFuture<Void> {
                self.error = error

                return task.eventLoop.makeSucceededFuture(())
            }
        }

        let localHTTPBin = HTTPBin(refusesConnections: true)
        let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
        defer {
            XCTAssertNoThrow(try localClient.syncShutdown())
            XCTAssertNoThrow(try localHTTPBin.shutdown())
        }

        let delegate = TestDelegate()

        do {
            let request = try HTTPClient.Request(url: "http://localhost:\(localHTTPBin.port)/get")
            try localClient.execute(request: request, delegate: delegate).wait()
            XCTFail("Shouldn't succeed")
        } catch {
            print("error: \(error)")
            print("delegate.error: \(String(describing: delegate.error))")
            XCTAssert(delegate.error != nil)
        }
    }

error: HTTPClientError.remoteConnectionClosed
delegate.error: nil
/data/async-http-client/Tests/AsyncHTTPClientTests/HTTPClientTests.swift:1569: error: HTTPClientTests.foo : XCTAssertTrue failed -

Metadata

Metadata

Assignees

Labels

kind/bugFeature doesn't work as expected.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions