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

interceptor not working with expressAdapter with Error in Guard #3065

Closed
vanhumbeecka opened this issue Sep 30, 2019 · 5 comments
Closed

interceptor not working with expressAdapter with Error in Guard #3065

vanhumbeecka opened this issue Sep 30, 2019 · 5 comments

Comments

@vanhumbeecka
Copy link

vanhumbeecka commented Sep 30, 2019

Bug Report

Hi, I'm having issued with interceptor who aren't triggered when throwing a custom exception in a Guard. The setup of the nest application uses the ExpressAdapter from the nestjs/platform-express package

Current behavior

  • Throw a a custom exception in a guard (which extends from Error)

  • Both globally/locally registered custom error-Interceptor (like in the docs) is never reached.

  • The global nest-built-in catch-all filter triggers and throws a 500 exception.

  • When testing this exception with a global registered Filter instead of Interceptor, everythings works, and the Filter behaves as expected.

  • Behaviour is also OK, when throwing an error OUTSIDE of a guard, for example in the controller itself.

Input Code

const app = await NestFactory.create<NestExpressApplication>(AppModule, new ExpressAdapter(expressApp));

// NEST CONFIG: error handling
app.useGlobalInterceptors(new ErrorsInterceptor()); // not working... :/
app.useGlobalFilters(new ForbiddenOperationFilter()); // works.

The custom errorinterceptor:

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {

    intercept(
        context: ExecutionContext,
        next: CallHandler
    ): Observable<any> {
        return next
            .handle()
            .pipe(
                catchError(err => {

                    Logging.instance.error(err.message)
                    // never reached...
                    ...
            );
    }
}

Expected behavior

The globally interceptor should be reached when throwing an Error within a Guard

Possible Solution

  • Maybe something todo with ExpressAdapter?

Environment


Nest version: 6.7.2
 
For Tooling issues:
- Node version: 8.10
- Platform:  Mac OS Mojave

Others:

@vanhumbeecka vanhumbeecka added the needs triage This issue has not been looked into label Sep 30, 2019
@kamilmysliwiec
Copy link
Member

Please, provide a minimal repository which reproduces your issue.

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Sep 30, 2019
@vanhumbeecka
Copy link
Author

I've updated the bug report.
Link to repo: https://github.com/vanhumbeecka/nest-bug
After installing dependencies, run npm run offline.
You can toggle the `interceptor' and/or filter in the 'main.ts'

@vanhumbeecka vanhumbeecka changed the title interceptor not working with expressAdapter interceptor not working with expressAdapter with Error in Guard Sep 30, 2019
@kamilmysliwiec
Copy link
Member

Interceptors are being evaluated after guards. You can't catch error thrown by guard inside an interceptor.

@vanhumbeecka
Copy link
Author

Ok, thanks for the clarification

@lock
Copy link

lock bot commented Dec 29, 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 Dec 29, 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