Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

senrab's avatar

I can't catch a HTTP Client exception

Hi all,

I am using the new(-ish) HTTP Client, something like this $response = Http::get($listToCheck->url);

I've actually stripped it back from $response = Http::retry(3, 500)->timeout(10)->get($listToCheck->url);

I am trying to catch the exception and store it in the DB. So if I run my code with $response = Http::get($listToCheck->url);to an URL that doesn't exist, I am getting a Illuminate\Http\Client\ConnectionException error thrown and the console command exits with this exception

cURL error 6: Could not resolve host: this.is.a.forced.fail.com

however I can't seem to catch that error and continue the code to insert the error into the DB The docs say $response->throw();

Here's the code

foreach ($listsToCheck->get() as $listToCheck) {
               $response = Http::get($listToCheck->url);
               $response->throw();
               dd('here');
...

All I see is the exception, I don't get the dd.

What am I missing?

0 likes
13 replies
laracoft's avatar

@senrab you need a try catch wrapper. Do you have it?

foreach ($listsToCheck->get() as $listToCheck) {
	try{
		$response = Http::get($listToCheck->url);
	} catch (\Exception $e)
	{
		// handle the exception
	}
              
}
senrab's avatar

I did, however I will try again :)

senrab's avatar

Thanks @laracoft, that got me a little further.

Where would I use $response->throw() in that catch() as I won't have access to $response there?

laracoft's avatar

@senrab

Which info do you want from $response? The docs don't say much, but if it was a 400 or 500 error, it won't throw exceptions, you check for them right after the Http::get() https://laravel.com/docs/8.x/http-client#error-handling

In your case of an invalid domain, that info can be read off Illuminate\Http\Client\ConnectionException, there won't be any headers or body, so a $response is of no use.

You have access to the URL via $listToCheck->url.

senrab's avatar

I was hoping to get $response->status() at least so I can log that in the database as a failed attempt.

The docs also promise $response->serverError() or $response->clientError(), but I don't seem to have access to them after the exception.

Pretty sure guzzle would give me the status code after an exception.

laracoft's avatar

@senrab

  1. ->status() usually refers to the HTTP code returned by the server.
  2. If your case, the server did not response at all. There isn't any ->serverError().
  3. Anyway, where did you see ->serverError() and ->clientError()?
  4. I checked the Facade, vendor\laravel\framework\src\Illuminate\Http\Client\Factory.php, it does not provide much.
  5. You could use guzzle directly (That's what I'm doing in my own Laravel 5/6 code)
senrab's avatar

@burfi I don't think I ever solved it the way I wanted - we were hoping to store the failure code in the DB however we just skipped that and silently handled the error.

laracoft's avatar

@senrab those functions are to be used this way:

foreach ($listsToCheck->get() as $listToCheck) {
    try {
        { // option A
            $response = Http::get($listToCheck->url);

            // Determine if the status code was >= 200 and < 300...
            if (!$response->successful())
            {
                $response->throw();
            }
        }

        { // or option B
            $response = Http::get($listToCheck->url);

            // Determine if the status code was >= 400...
            $c1 = $response->failed();

            // Determine if the response has a 400 level status code...
            $c2 = $response->clientError();

            // Determine if the response has a 500 level status code...
            $c3 = $response->serverError();

            if ($c1 or $c2 or $c3)
                $response->throw();
        }
    } catch (\Exception $e)
    {
	// handle other exceptions such as ConnectionException
    }
}
Shaden's avatar

One Way to do this, is use your own custom Exception and pass the response reason phrase.


 if ($response->successful()) {
            return $response->json();

        }

        if ($response->failed()) {
            throw new LoginFailedException($response->getReasonPhrase());
        };

2 likes

Please or to participate in this conversation.