core: Self-handling exceptions should not have priority in Exception handler
Exception handler should be a central place to define custom handlers for exceptions thrown in application.
Yet, turns out that exceptions with handle()
method have higher priority than custom handlers:
To handle such exception it’s required to add a custom hook.
IMO handle()
method of exception should have lower priority then custom handlers bound in main handler.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 46 (35 by maintainers)
I believe we should have an
ExceptionHandler
that will reside on the core of the framework.This will check if the exception can handle itself as @radmen propose and fallback to the Youch screen if it’s not possible.
Then, the
ExceptionHandler
created by the developer/cli (let’s sayCustomExceptionHandler
) will callsuper.handle(error, cli)
to hook over every single exception.The flow will be basically.
CustomExceptionHandler
available?CustomExceptionHandler.handle()
, if the exception is not hooked here -->super.handle()
ExceptionHandler.handle()
, self-handled?, Fallback to Youch error pageThis provides a good way to hook overall exceptions you can have on the system and doesn’t feel like magic.
Okay, So I am sync with you guys now. I will working on it in a way that it should not introduce any breaking changes, since every app will be using custom exception handlers.
@radmen Thanks for staying patient 😄
@swkimA Yes this is what going to happen in the future. For now I recommend using the workaround mentioned here https://github.com/adonisjs/adonis-framework/issues/718#issuecomment-350831415
Actually what I wanted to say is that the Exception handler should make use of exceptions
handle()
method.Something like:
This way all error handling goes through the handler and is redirected to exceptions
handle()
methods.Having this, developer will only have to overwrite
handle()
method of ExceptionHandler if they want different results.Overall I’d say that this will change the priority of error handling yet, the behaviour (for defaults) will remain the same.
@AyozeVera the whole point of this discussion is that I suggest that exception handler should unify handling of all exceptions (like in Laravel) 😃
If you’re defining own exceptions generic exception handler will be enough. Just create new one (using
adonis make:ehandler
) and define response format inhandle()
method.For Adonis bultin exceptions (specially the ones which are self-handling) you need to define custom handlers.
Here’s the excerpt of
start/hooks.js
file from one of mine projects. Basically, you can copy&paste it to your project 😃To be complete, here’re contents of
App/Exceptions/Handler/genericExceptionsHandler