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

feat(@nestjs/graphql): base exception filter for apollo exceptions #1292

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/graphql-base-exception-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import {
ApolloError,
AuthenticationError,
ForbiddenError,
} from 'apollo-server-errors';
import { GqlContextType } from './services';

const apolloPredefinedExceptions: Record<number, typeof ApolloError> = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why UserInputError and other ApolloErrors are not included?

401: AuthenticationError,
403: ForbiddenError,
};

@Catch(HttpException)
export class GraphQLBaseExceptionFilter extends BaseExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
if (host.getType<GqlContextType>() !== 'graphql') {
return null;
}

let error: ApolloError;
if (exception.getStatus() in apolloPredefinedExceptions) {
error = new apolloPredefinedExceptions[exception.getStatus()](
exception.message,
);
} else {
error = new ApolloError(
exception.message,
exception.getStatus().toString(),
);
}

error.stack = exception.stack;
error.extensions['response'] = exception.getResponse();

throw error;
}
}
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './decorators';
export * from './federation';
export * from './graphql-ast.explorer';
export * from './graphql-definitions.factory';
export * from './graphql-base-exception-filter';
export * from './graphql-schema.host';
export * from './graphql-types.loader';
export * from './graphql.factory';
Expand Down
10 changes: 8 additions & 2 deletions tests/e2e/guards-filters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { GraphQLBaseExceptionFilter } from '../../lib';
import { ApplicationModule } from '../code-first/app.module';

describe('GraphQL - Guards', () => {
Expand All @@ -12,6 +13,7 @@ describe('GraphQL - Guards', () => {
}).compile();

app = module.createNestApplication();
app.useGlobalFilters(new GraphQLBaseExceptionFilter());
await app.init();
});

Expand All @@ -26,7 +28,7 @@ describe('GraphQL - Guards', () => {
.expect(200, {
errors: [
{
message: 'Unauthorized error',
message: 'Unauthorized',
locations: [
{
line: 2,
Expand All @@ -35,7 +37,11 @@ describe('GraphQL - Guards', () => {
],
path: ['recipe'],
extensions: {
code: 'INTERNAL_SERVER_ERROR',
code: 'UNAUTHENTICATED',
response: {
statusCode: 401,
message: 'Unauthorized',
},
},
},
],
Expand Down
20 changes: 9 additions & 11 deletions tests/e2e/pipes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { GraphQLBaseExceptionFilter } from '../../lib';
import { ApplicationModule } from '../code-first/app.module';

describe('GraphQL - Pipes', () => {
Expand All @@ -12,6 +13,7 @@ describe('GraphQL - Pipes', () => {
}).compile();

app = module.createNestApplication();
app.useGlobalFilters(new GraphQLBaseExceptionFilter());
app.useGlobalPipes(new ValidationPipe());
await app.init();
});
Expand All @@ -30,17 +32,13 @@ describe('GraphQL - Pipes', () => {
errors: [
{
extensions: {
code: 'INTERNAL_SERVER_ERROR',
exception: {
message: 'Bad Request Exception',
response: {
error: 'Bad Request',
message: [
'description must be longer than or equal to 30 characters',
],
statusCode: 400,
},
status: 400,
code: '400',
response: {
error: 'Bad Request',
message: [
'description must be longer than or equal to 30 characters',
],
statusCode: 400,
},
},
locations: [
Expand Down