nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.73k stars 7.63k forks source link

Only the last APP_FILTER providers works #13353

Closed xadamxk closed 7 months ago

xadamxk commented 7 months ago

Is there an existing issue for this?

Current behavior

If more than one APP_FILTER providers are provided, only the last one gets triggered.

Minimum reproduction code

https://github.com/xadamxk/nestjs-filterbug-reproduction https://gist.github.com/xadamxk/0d4648a79e708b6957b976ae54b3b271

Steps to reproduce

  1. Throw an error the AuthExceptionFilter is registered to catch (ie. ForbiddenException) in a controller
  2. Rather than returning "Unauthorized", like expected, the controller will return "Internal Server Error"
  3. console.log("in auth-filter"); is never ran, but console.log("in global-filter"); is

Expected behavior

I would expect the first filter (AuthExceptionFilter) to run if an exception matches it's provided exception list. And if not, the next/last filter (GlobalExceptionFilter) would run.

Package

Other package

No response

NestJS version

10.3.5

Packages versions

platform-express version : 10.3.5
platform-fastify version : 10.3.5
schematics version       : 10.1.1
terminus version         : 10.2.3
swagger version          : 7.3.0
typeorm version          : 10.0.2
testing version          : 10.3.5
common version           : 10.3.5
config version           : 3.2.0
core version             : 10.3.5
cli version              : 10.3.2

Node.js version

18.19.1 (tried 20.x as well)

In which operating systems have you tested?

Other

I found the same issue from 6 years ago, which was said to be fixed in 5.1.0, but I am running into the exact same problem now.

micalevisk commented 7 months ago

Please provide a minimum reproduction repository. You can start one by running npm init nest in your terminal

why reproductions are required

xadamxk commented 7 months ago

@micalevisk I've updated the original issue to include a complete sample project: https://github.com/xadamxk/nestjs-filterbug-reproduction.

kamilmysliwiec commented 7 months ago

https://github.com/nestjs/nest/issues/9372 https://github.com/nestjs/nest/issues/3252

This is the expected behavior (filters execution is not chained; only 1 is executed per exception)

xadamxk commented 7 months ago

This is the expected behavior (filters execution is not chained; only 1 is executed per exception)

I don't expect the filter execution to be chained, but I do expect the application to pass the exception to a filter if the specified exception matches. In my example, only 1 filter is able to be used in my application while the documentation states that multiple filters are supported.

You can add as many filters with this technique as needed; simply add each to the providers array.

The auth.filter in my sample project should be triggering and it isn't. Am I misunderstanding or is that actually expected? Thanks for your help.

xadamxk commented 5 months ago

@micalevisk @kamilmysliwiec could you review my most recent comment please?

y-nk commented 3 months ago

It'd be nice if the error would be back in the flow of filters when the catch() would again throw exception (or return Promise.reject(exception))