Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add interface to override routing-controllers' default error handler #769

Open
GeekEast opened this issue Aug 30, 2021 · 1 comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@GeekEast
Copy link

Description

Currently routing-controllers' defaultErrorHandler is enabled by default.

If I want to do some error logging and error filtering using middleware, it can only happen after the error is sent to the client.

error filtering example

if (error instanceof JsonWebTokenError) return next(new UnauthorizedError(error.message))

I tried this approach

  • turn off defaultErrorHandler
  • write my own custom error handler middleware

But I got some issues:

  • I cannot confirm that the error throw behavior is the same as before.

So I tried to replicate the default error handler, but I find it's quite complicated to override since it's not easy to get the actionMetadata object in middleware.

Proposed solution

I would suggest providing an interface like this to override the default error handler.

export class CustomErrorHandler {
     handle(error:any, action: ActionMetadata | undefined, options: Action){
          // developer can customer behaviours here.
     }
}
@GeekEast GeekEast added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Aug 30, 2021
@attilaorosz
Copy link
Member

attilaorosz commented Feb 19, 2022

If you add your own error handler it is quite different. You are basically registering a normal ExpressErrorMiddlewareInterface by using your own. The downside is of course you have no access to the action and options.

A possible solution would be to extend the thrown error by the metadata required for the default error handler and pass that to the custom error handler instead. We could create a new base class for errors with the processJsonError and processTextError methods included. That way the users can just use the built in stuff even in their custom error handlers if they choose to.

@NoNameProvided What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

No branches or pull requests

2 participants