Skip to content

Convert SocketException to guzzle ConnectException #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from

Conversation

luzrain
Copy link

@luzrain luzrain commented Feb 24, 2025

Convert Amp\Http\Client\SocketException to guzzle GuzzleHttp\Exception\ConnectException

Comment on lines 143 to 144
} catch (SocketException $e) {
$promise->reject(new ConnectException($e->getMessage(), $request, $e));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is not always right, e.g. if a socket is closed while the request is sent, we likely don't want a ConnectException?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it might be better to handle this the same way as in CurlFactory.php, where only certain types of errors are converted to ConnectException, while all others are converted to RequestException.
But all raw SocketException should definitely be converted to either ConnectException or RequestException to be compatible with Guzzle, which always throws its own internal exceptions in such cases. Applications rely on these Guzzle exceptions, and they must remain consistent no matter what handler is used under the hood.

The only problem is that we cannot differentiate between SocketException cases. We need to add some form of differentiation to implement this properly.
See my related issue about this: amphp/http-client#379

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kelunik
Could you please take another look at this PR?
I've made some update, now, timeout errors and DNS resolution errors (when possible to distinct them) are wrapped into ConnectException, while all other exceptions are wrapped into RequestException.

This behavior aligns with Guzzle default handlers, which also wrap exceptions into either ConnectException or RequestException.
For reference, see the implementations in CurlFactory and StreamHandler.

…on, all other exceptions to RequestException.
@luzrain luzrain force-pushed the connect_exception branch from 636688a to 8068f2c Compare April 3, 2025 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants