api: Custom ExceptionHandler
Is it possible to overwrite the exception handler? If I would like to add custom syntax for the errors responses. Would be awesome if we could handle the entire exception handler outside of this package and use the default one that comes with the package out of the box.
Dingo\Api\Routing\Router::__construct() must be an instance of Dingo\Api\Exception\Handler, instance of App\Exceptions\Handler given,
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 56 (34 by maintainers)
Finally! Sorry to post on closed issue, but I must share this solution somewhere, I lost so many hours on it… First of all create your own exception handler:
app/Exceptions/ApiExceptionsHandler.phpThen create wrapper for Laravel Dingo service provider:
app/Providers/DingoServiceProvider.phpLastly replace original Dingo service provider to this wrapper:
/config/app.comThat’s it. Hope it’ll help someone.
Hi guys
It’s actually very easy to override the dingo exception handler. Just make a new exception handler class in your app, which extends the dingo one, and do the following in your AppServiceProvider
In this case, the ApiExceptionHandler is my custom exception handler. And so in extending dingo’s one, you are free to override any methods you wish.
And the class you want to extend is Dingo\Api\Exception\Handler
@shanginn thanks a lot, it work great.
I want to map all thrown exceptions in this packages to my Laravel exception handler class
App\Exceptions\Handlerinstead of using the exception handler in this package. Since I want custom syntax for my API responses.In case you are looking for a more robust error handler where jwt is included. I have modified the ApiExceptionsHandler as shown in the code below.
I hope someone finds this helpful. Cheers!!!
@shanginn after doing this, I’m getting 500 response and no response data 😦
@jasonlewis I am quite new to Laravel and I am trying to rebind api.exception and implement totally custom exception handler. I can’t get it work.
I have custom exception handler like this:
I have registered the handler in
AppServiceProvider's register() method:The handle() method never get’s called. What am I doing wrong?
Thanks!
Well… you could rebind the Dingo exception handler as well. But what I was suggesting is to type-hint the standard
Exceptionclass in the Dingo exception handler and then pass the exception on to your applications handler.