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

Global interceptor's tap function is not executed if exception has been thrown #1337

Closed
weeco opened this issue Dec 3, 2018 · 5 comments
Closed

Comments

@weeco
Copy link

weeco commented Dec 3, 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

I created a global interceptor (literally copy pasted from the interceptor docs) and applied it as global interceptor to my NestJS app:

import { ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable } from 'rxjs';
// tslint:disable-next-line:no-submodule-imports
import { tap } from 'rxjs/operators';

@Injectable()
export class GlobalMetricsInterceptor implements NestInterceptor {
  public intercept(context: ExecutionContext, call$: Observable<unknown>): Observable<unknown> {
    console.log('Before...');

    const now: number = Date.now();

    return call$.pipe(tap(() => console.log(`After... ${Date.now() - now}ms`)));
  }
}

If an exception is thrown on a called route the "After..." logline will never be printed:

Before...
After... 0ms
Before...
After... 0ms
Before...
After... 0ms
Before...
{"level":60,"time":1543875905200,"msg":"Exception in 'getPlayerProfile'. ECONNREFUSED - connect ECONNREFUSED 127.0.0.1:4400'","name":"ApiClientService","severity":"ERROR","v":1}

Expected behavior

I expected it to always print before and after.

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

I want to create prometheus metrics (#967 ) for all routes (e. g. HTTP response duration grouped by HTTP status code) and therefore I always need it to execute a function afterwards.

Environment


Nest version: 5.4.1

 
For Tooling issues:
- Node version: 10
- Platform: Windows
@marcus-sa
Copy link

marcus-sa commented Dec 4, 2018

tap is not supposed to be called if an error was thrown.
You need to combine it with the catchError operator.
https://www.learnrxjs.io/operators/error_handling/catch.html

@weeco
Copy link
Author

weeco commented Dec 4, 2018

@marcus-sa Is there no operator which would execute a function regardless if the "middleware" throws or not?

@kamilmysliwiec
Copy link
Member

tap takes 3 parameters, next, error, and complete. If you want to capture errors as well, just add another function (second param)

@weeco
Copy link
Author

weeco commented Dec 4, 2018

Apparently finalize() offers what I was looking for. Thank you for your comments though!

Happy Birthday @kamilmysliwiec

@weeco weeco closed this as completed Dec 4, 2018
@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants