Closed
Description
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity
iTearo commentedon Feb 25, 2025
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 isSocketException
instead of the expectedTimeoutException
.PS: I use
amphp/http-client-guzzle-adapter
.iTearo commentedon Feb 25, 2025
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 commentedon Feb 27, 2025
Why do you need to differentiate this specific error?
luzrain commentedon Feb 28, 2025
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 commentedon Feb 28, 2025
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 whichcurl
returns the error codeCURLE_OPERATION_TIMEDOUT (28)