I'd like to catch all ordinary exceptions (instances of Exception class) from one of my controllers (or in future in several controllers) to unify their behavior. I know how to make global handlers for exceptions in Exceptions/Handler.php, but how can I limit them to some particular controller?
What I want to do is to return such an array in JSON format whenever Exception is being thrown in my API controller:
[
    'error' => 'Internal error occurred.'
]
I could decide to throw my own exception class, perhaps ApiException, but I want to serve third party exceptions as well, such as database errors.
Should I pass some value to the request object directly? If so, how? Or maybe there's another way?
If you want to render a different type of exception for a specific controller, you can use the request object to check the current controller :
Exceptions/Handler.php
public function render($request, Exception $e)
{
    if($request->route()->getAction()["controller"] == "App\Http\Controllers\ApiController@index"){
        return response()->json(["error" => "An internal error occured"]);
    }
    return parent::render($request, $e);
}
                        You can do this:
create an exception class
class APIException extends Exception{
}
then throw it from the controller
throw new APIException('api exception');
and catch it from Exceptions/Handler.php
public function render($request, Exception $e)
{
    if ($e instanceof APIException){
        return response(['success' => false, 'data' => [], 'message' => $e->getMessage(), 401);
    }
    if ($e instanceof SomeException){
        return response(['success' => false, 'data' => [], 'message' => 'Exception'], 401);
    }
    return parent::render($request, $e);
}
                        You can also filter by the request by their path patterns.
Go to the file app\Exceptions\Handler.php:
public function render($request, \Exception $e)
{
    /* Filter the requests made on the API path */
    if ($request->is('api/*')) {
        return response()->json(["error" => "An internal error occurred"]);
    }
    return parent::render($request, $e);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With