Skip to content

Commit

Permalink
Merge branch 'exceptions' of https://github.com/nirga/graphql into ni…
Browse files Browse the repository at this point in the history
…rga-exceptions
  • Loading branch information
kamilmysliwiec committed Mar 19, 2021
2 parents a412984 + 422d392 commit e98e966
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
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> = {
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

0 comments on commit e98e966

Please sign in to comment.