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 · 3 comments
Closed

Exceptions are not handled in cascade way #729

adrianbudzynski opened this issue May 24, 2018 · 3 comments

Comments

@adrianbudzynski
Copy link

adrianbudzynski 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
Copy link
Author

adrianbudzynski 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
Copy link
Member

Fixed in v5.0.1. Thanks @adrianbudzynski 🙂

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants