Laravel error before laravel does

I am using Laravel 5.1 and Guzzle to send API requests.

I have two functions, one to get the person, and the second to get the data about the person, since each is a separate request.

If nothing is found for the 404 response code, it is sent back and taken by the laravel.

However, the 404 response from function 1 has a different meaning than the response from function two, although they are the same response code and exception, but a bundle.

I tried to catch the error in the controller for the method in the hope that it would catch it before the laravels exception handler is executed, but it doesn't seem to work and is not caught by the handler.

how can I catch the exception in the controller before the Laravels Exception handler?

+3


source to share


1 answer


At the top of the controller add:

use GuzzleHttp\Exception\RequestException;

      

Wrap your request in an attempt to catch like this:



try {
    $client->get('https://github.com/_abc_123_404');
} catch (RequestException $e) {
    echo $e->getRequest();
    if ($e->hasResponse()) {
        echo $e->getResponse();
    }
}

      

You can catch any Guzzle exception if you only want to catch 404, then you can try to use ClientException

at the top of your controller and catch this, this exception is propagated to BadResponseException

, which in turn extends RequestException

. For details see.

+6


source







All Articles