Skip to content

SocketException distinction #379

Closed
@luzrain

Description

@luzrain
Contributor

There is an Amp\Http\Client\SocketException exception that covers different types of connection errors. However, I found out that it's not possible to distinguish between them programmatically, they are all the same except for the error message.
I need to differentiate a specific error, and the only way to do that is to check if the error message contains a substring, which is not reliable.
What do you think we can do about this? My first thought is to add unique exception codes to all SocketException cases.

Activity

iTearo

iTearo commented on Feb 25, 2025

@iTearo

I agree with the problem. In my case I get an exception with the text Receiving the response headers for ‘http://localhost:8080/’ failed, because the socket to ‘localhost:8080’ @ ‘???’ closed early with 0 bytes received within 10.001 seconds
My request has a timeout of 10 seconds $httpClient->request('GET', 'http://localhost:8080', ['timeout' => 10, 'http_errors' => true]) , but the exception I encounter is SocketException instead of the expected TimeoutException.

PS: I use amphp/http-client-guzzle-adapter.

iTearo

iTearo commented on Feb 25, 2025

@iTearo

Moreover, there is an old issue telling us that it is exactly the timeout error, thus it may be distincted to TimeoutException #286 (comment)

kelunik

kelunik commented on Feb 27, 2025

@kelunik
Member

Why do you need to differentiate this specific error?

luzrain

luzrain commented on Feb 28, 2025

@luzrain
ContributorAuthor

We need to differentiate error types in our application. With curl, this is possible because it provides error codes along with errors. However, with amp/http-client, all errors are simply SocketException with only different error messages.

This also needs to be done to properly convert SocketException into guzzle internal exceptions in amphp/http-client-guzzle-adapter.
amphp/http-client-guzzle-adapter#3

iTearo

iTearo commented on Feb 28, 2025

@iTearo

I'm assuming that in my case, the error text closed early with 0 bytes received within 10.001 seconds is same as a case for which curl returns the error code CURLE_OPERATION_TIMEDOUT (28)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @kelunik@iTearo@luzrain

      Issue actions

        SocketException distinction · Issue #379 · amphp/http-client