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

Exceptions are not handled in cascade way #729

Closed
adrianbudzynski opened this issue May 24, 2018 · 2 comments

Comments

@adrianbudzynski
Copy link

commented May 24, 2018

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Annotated controller API method by @UseFilters(NotFoundExceptionFilter) is ignored and used is a global exception handler. After global exception filter is commented out, NotFoundExceptionFilter works as expected.

const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new AnyExceptionFilter());

or

providers: [AppService,
        {
            provide: APP_FILTER,
            useClass: AnyExceptionFilter,
        },
    ],

Expected behavior

Controller annotated exception filters should have higher priority than global exception filter.

Minimal reproduction of the problem with instructions

async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useGlobalFilters(new AnyExceptionFilter());
    await app.listen(process.env.PORT || 3000);
}
@Controller()
export class TestController {

    constructor() {
    }

    @Get('/')
    @Render('view')
    @UseFilters(NotFoundExceptionFilter)
    async index(@Param() params, @Res() res, @Headers() headers): Promise<any> {
        throw new NotFoundException();
    }
}
@Catch()
export class AnyExceptionFilter implements ExceptionFilter {
    catch(exception: any, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();

        response
            .status(500)
            .render('error/any-exception', {});
    }
}
@Catch(NotFoundException)
export class NotFoundExceptionFilter implements ExceptionFilter {

    catch(exception: any, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();

        response
            .status(404)
            .render('error/not-found-exception');
    }
}

What is the motivation / use case for changing the behavior?

I want to have global exception handler for any exception, and I want to override exception handler for just specific one controller for one exception type.

Environment


Nest version: 5.0.0

 
For Tooling issues:
- Node version: v9.4.0
- Platform:  Mac

Others:

@adrianbudzynski

This comment has been minimized.

Copy link
Author

commented May 24, 2018

I think to resolve that issue, filters array used in invokeCustomFilters method in exceptions-handler.ts class should be reversed while it is initialized in setCustomFilters method in the same class. I am not sure if this change won't cause any additional problems, please correct me if I am wrong.

public invokeCustomFilters(exception, response): boolean {
    if (isEmpty(this.filters)) return false;

    const filter = this.filters.find(({ exceptionMetatypes, func }) => {
      const hasMetatype =
        !exceptionMetatypes.length ||
        !!exceptionMetatypes.find(
          ExceptionMetatype => exception instanceof ExceptionMetatype,
        );
      return hasMetatype;
    });
    filter && filter.func(exception, response);
    return !!filter;
  }
@kamilmysliwiec

This comment has been minimized.

Copy link
Member

commented May 25, 2018

Fixed in v5.0.1. Thanks @adrianbudzynski 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.