4

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 Illuminate\Support\Facades\Http;
    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

Illuminate\Http\Client\ConnectionException
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?

1 Answer 1

7

This is almost certainly a namespacing issue.

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

use Illuminate\Http\Client\ConnectionException;

or do this:

} catch(\Illuminate\Http\Client\ConnectionException $e)

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, only the second method works but I would prefer the first, do you know why?
No; I'd suspect you made a mistake somewhere (typo?) if that's the case.
Although I am on Laravel 9, in my case both of them fail... :( I see local.ERROR: cURL error 6: Could not resolve host: and my catch block not working... Any pointers would be much appreciated. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.