Skip to content
Advertisement

Catch HTTP client errors in Laravel 8

How do you catch errors thrown by the HTTP client (for example a time out) so that it doesn’t throw the curl error in the Laraval debugger (in debug mode) before you can do anything with the error to avoid stopping the execution?

    use IlluminateSupportFacadesHttp;
    try {
        $request = Http::post('https://example.com/post', [
        'password' => 'guest']);

    } catch(ConnectException $e)
    {
        //log error
    }

    //continue with another mode

Instead, I’m always getting the Laravel’s Ignition error page

IlluminateHttpClientConnectionException
cURL error 28: Failed to connect to example.com port 443: Timed out

and the error is not caught by my code. Is it possible that the laravel debugger always have priority and can’t be overridden in debug mode?

Advertisement

Answer

This is almost certainly a namespacing issue.

You’ll need either this at the top of the file:

use IlluminateHttpClientConnectionException;

or do this:

} catch(IlluminateHttpClientConnectionException $e)

Otherwise, you’re actually trying to catch something in the current namespace named ConnectionException (i.e. something like AppControllersConnectionException), which will never exist.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement