Skip to content
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

[HttpClient] - Cannot catch TransportException error #34281

Closed
arvilmena opened this issue Nov 7, 2019 · 8 comments
Closed

[HttpClient] - Cannot catch TransportException error #34281

arvilmena opened this issue Nov 7, 2019 · 8 comments

Comments

@arvilmena
Copy link

arvilmena commented Nov 7, 2019

Symfony version(s) affected:

symfony/http-client v4.3.4

Description

I am having trouble catching Symfony\Component\HttpClient\Exception\TransportException when doing a GET request on servers without SSL installed.

How to reproduce

Trying to catch, \Symfony\Component\HttpClient\Exception\TransportException nor \Exception nor \Throwable doesn't seem to help.

$client = HttpClient::create();

try {
	$response = $client->request( 'GET', 'https://xxxx.xxxx' );
} catch ( \Symfony\Component\HttpClient\Exception\TransportException | \Exception | \Throwable $exception ) {
	die( $exception->getMessage() );
}

Still throws Fatal error: https://pastebin.com/raw/edfnSeu5

PS

In case it might help, I have the following packages too:

composer show https://pastebin.com/raw/mXJSxqQj
composer.json https://pastebin.com/raw/05PbqDZS

Hope somebody can help.

@nicolas-grekas
Copy link
Member

As stated in the doc, responses are lazy: https://symfony.com/doc/current/components/http_client.html

The exception happens after the call to request(), at destruction time - ie outside of the try/catch.
Call any methods on $response (exception getInfo()) - that will make the client wait for the network, thus throw.

@arvilmena
Copy link
Author

Ahhh, feels weird, but got it now. Thanks!

@stof
Copy link
Member

stof commented Nov 7, 2019

Well, the goal of lazyness is that you can start multiple requests, and waits only after that. This way, the network can run them in parallel.

@er1z
Copy link

er1z commented Dec 12, 2019

Do you think that is there a room for documentation improvement? How do you recommend to catch-up the exceptions within lazy approach?

So far, I'm using:

try{
  $response = $httpClient->request(…);
  $response->getStatusCode();
  return $response;
}catch(…){
  //
}

@nicolas-grekas
Copy link
Member

Not this way @er1z - that disables a safety measure that requires ppl to write correct error handling code.

@er1z
Copy link

er1z commented Dec 12, 2019

@nicolas-grekas so what is a better way?

@nicolas-grekas
Copy link
Member

It depends on the use case: if one doesn't care about the response, there is no need to create a variable for it.
And if one cares, the error should be dealt with at the location where the response status (or the headers, or the body) is read and used.

@zahitrios
Copy link

I had the same error in Symfony5, and I solved adding use Exception; in the first lines of my class (In my case I was building a Service)

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

No branches or pull requests

6 participants