Request scoped factories exception filters not caught by GqlExceptionFilter
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
When throwing an exception inside of a request scoped factory, the exception will not be caught by exception filters. This might be related to the issue I filled in the nestjs/nest repository (https://github.com/nestjs/nest/issues/5173) - where exception filters thrown in request scoped factories will not terminate the response.
Expected behavior
When throwing an exception inside of a request scoped factory, exception filters will catch the exception.
Minimal reproduction of the problem with instructions
https://github.com/egodsk/nestjs-graphql-exception-filter
Start the app npm run start:dev, and hit the graphql endpoint POST localhost:3000/graphql with the following body's:
{
test
}
and
{
test2
}
The test query will result in an error which will not be caught by the exception filter, however the test2 query will correctly we caught by the exception filter.
What is the motivation / use case for changing the behavior?
I would like to be able to throw request scoped exceptions inside of factories and then catch them with Exception Filters to convert them to ApolloError syntax.
Environment
Nest version: 7.4.2
For Tooling issues:
- Node version: v.12.18.2
- Platform: Mac
In case the minimal repo does not show what is going on clearly enough, this is roughly what is going on:
Having a request scoped provider factory, which throws a custom exception inside, will not be caught by global graphql exception filters.
For example: Defining a factory provider, which fails by throwing a custom exception,
{
provide: 'REQUEST_SCOPED_PROVIDER',
scope: Scope.REQUEST,
inject: [REQUEST],
useFactory: async (request: Request) => {
console.log('Request Scoped Provider - useFactory');
throw new CustomException();
},
},
With a global exception filter:
@Catch(CustomException)
export class CustomExceptionFilter implements GqlExceptionFilter {
public catch(): any {
console.log('not being called');
return new Error('custom error');
}
}
added to the application with app.useGlobalFilters(new CustomExceptionFilter()); will not be caught. In the console the message Request Scoped Provider - useFactory is correctly being logged, but the message not being called is not being logged.
The most minimal example I can create:
import {
Catch,
Inject,
Module,
Scope,
} from '@nestjs/common';
import { NestFactory, REQUEST } from '@nestjs/core';
import { GqlExceptionFilter, GraphQLModule, Query, Resolver } from '@nestjs/graphql';
@Resolver('App')
export class AppResolver {
constructor(
@Inject('REQUEST_SCOPED_PROVIDER')
private readonly requestScopedProvider
) {}
@Query(() => Boolean)
public test(): boolean {
return true;
}
}
@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
}),
],
providers: [
{
provide: 'REQUEST_SCOPED_PROVIDER',
scope: Scope.REQUEST,
inject: [REQUEST],
useFactory: () => {
console.log('Request Scoped Provider - useFactory');
throw new CustomException();
},
},
AppResolver
],
})
export class AppModule {}
export class CustomException extends Error {}
@Catch(CustomException)
export class CustomExceptionFilter implements GqlExceptionFilter {
public catch(): any {
console.log('not being called');
return new Error('custom error');
}
}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new CustomExceptionFilter());
await app.listen(3000);
}
bootstrap();
running the above code and hitting the graphql endpoint with the body {test} will do the trick.
Are you also using GraphQL Shield in your case? If so, I'm facing the exact same issue.