Problem with global and local interceptor execution
See original GitHub issueI’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
I am trying to apply a global and a local interceptor to a NestJS application. The global interceptor is used for tracing every call in the application, logging incoming requests and outgoing responses. The local interceptor is used for caching some responses.
Once applied, the execution should go as described next:
- First request to
http://localhost:3000/
is not cached, calls controller and saves response in cache - Second request to
http://localhost:3000/
replies from cache skipping controller
Which prints the following output:
Tracing (incoming) > Cache (incoming) > Controller > Tracing (outgoing) > Cache (outgoing) >
Tracing (incoming) > Cache (incoming) > Hit >
Here we observe two issues:
- Order of interceptors is not reversed for response!
- Tracing interceptor is not executed on response due to cache interceptor returning a new observer I guess
Expected behavior
Upon applying global and local interceptors, I would expect the execution to go like this:
Tracing (incoming) > Cache (incoming) > Controller > Cache (outgoing) > Tracing (outgoing) >
Tracing (incoming) > Cache (incoming) > Hit > Tracing (outgoing) >
Minimal reproduction of the problem with instructions
https://github.com/RecuencoJones/nestjs-interceptors
What is the motivation / use case for changing the behavior?
Consistent execution of interceptor pipeline
From https://github.com/nestjs/nest/issues/541#issuecomment-383287000:
Think about interceptors, they follow a simple request processing pipeline, being executed from global to concrete once the request wants to hit an end-handler, and then (in response pipeline), they are executed from concrete to global (if you attach some asynchronous/mapping logic inside them and so on).
Environment
Nest version: 5.2.2
For Tooling issues:
- Node version: 10.8.0
- Platform: OS X High Sierra
Others:
- npm version: 6.2.0
- IDE: Visual Studio Code
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top GitHub Comments
I have created a fix already, but it would be a breaking change anyway, so we have to wait with it until next major release 🙁
@kamilmysliwiec any update?