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?