question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with global and local interceptor execution

See original GitHub issue

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

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:

  1. First request to http://localhost:3000/ is not cached, calls controller and saves response in cache
  2. 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:

  1. Order of interceptors is not reversed for response!
  2. 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, Sep 4, 2018

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 🙁

1reaction
JumpLinkcommented, Oct 17, 2018

@kamilmysliwiec any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.intercept Problems - Gleb Bahmutov
This is the browser trying to be robust and retrying a failed network request. The browser sees a network error coming back from...
Read more >
NestJS - Use service inside Interceptor (not global interceptor)
I assume that SignInterceptor is part of the ApiModule : @Module({ imports: [SignModule], // Import the SignModule into the ApiModule.
Read more >
Interceptors | NestJS - A progressive Node.js framework
Each interceptor implements the intercept() method, which takes two arguments. The first one is the ExecutionContext instance (exactly the same object as ...
Read more >
nestjs global interceptor | The AI Search Engine You Control
Think about interceptors, they follow a simple request processing pipeline, being executed from global to concrete once the request wants to hit an...
Read more >
Setting up Axios Interceptors for all HTTP calls in an application
Automatically intercept all the requests and responses so you don't have ... in local storage, in session storage — wherever it makes sense....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found