Skip to content

Commit 7072504

Browse files
feat(apollo): add wrapper error classes
1 parent 741937c commit 7072504

File tree

7 files changed

+84
-1
lines changed

7 files changed

+84
-1
lines changed

packages/apollo/lib/drivers/apollo-base.driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ApolloDriverConfig } from '../interfaces';
1414
import { createAsyncIterator } from '../utils/async-iterator.util';
1515

1616
const apolloPredefinedExceptions: Partial<Record<HttpStatus, string>> = {
17-
[HttpStatus.BAD_REQUEST]: ApolloServerErrorCode.BAD_USER_INPUT,
17+
[HttpStatus.BAD_REQUEST]: ApolloServerErrorCode.BAD_REQUEST,
1818
[HttpStatus.UNAUTHORIZED]: 'UNAUTHENTICATED',
1919
[HttpStatus.FORBIDDEN]: 'FORBIDDEN',
2020
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { GraphQLError, GraphQLErrorOptions } from 'graphql';
2+
3+
/**
4+
* This error is thrown when the user is not authenticated.
5+
*
6+
* "AuthenticationError" class was removed in the latest version of Apollo Server (4.0.0)
7+
* It was moved to the @nestjs/apollo package to avoid regressions & make migration easier.
8+
*/
9+
export class AuthenticationError extends GraphQLError {
10+
constructor(message: string, options?: GraphQLErrorOptions) {
11+
super(message, {
12+
...options,
13+
extensions: {
14+
code: 'UNAUTHENTICATED',
15+
...options?.extensions,
16+
},
17+
});
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { GraphQLError, GraphQLErrorOptions } from 'graphql';
2+
3+
/**
4+
* This error is thrown when the user is not authorized to access a resource.
5+
*
6+
* "ForbiddenError" class was removed in the latest version of Apollo Server (4.0.0)
7+
* It was moved to the @nestjs/apollo package to avoid regressions & make migration easier.
8+
*/
9+
export class ForbiddenError extends GraphQLError {
10+
constructor(message: string, options?: GraphQLErrorOptions) {
11+
super(message, {
12+
...options,
13+
extensions: {
14+
code: 'FORBIDDEN',
15+
...options?.extensions,
16+
},
17+
});
18+
}
19+
}

packages/apollo/lib/errors/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './authentication.error';
2+
export * from './forbidden.error';
3+
export * from './user-input.error';
4+
export * from './validation.error';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ApolloServerErrorCode } from '@apollo/server/errors';
2+
import { GraphQLError, GraphQLErrorOptions } from 'graphql';
3+
4+
/**
5+
* This error is thrown when the user input is invalid.
6+
*
7+
* "UserInputError" class was removed in the latest version of Apollo Server (4.0.0)
8+
* It was moved to the @nestjs/apollo package to avoid regressions & make migration easier.
9+
*/
10+
export class UserInputError extends GraphQLError {
11+
constructor(message: string, options?: GraphQLErrorOptions) {
12+
super(message, {
13+
...options,
14+
extensions: {
15+
code: ApolloServerErrorCode.BAD_USER_INPUT,
16+
...options?.extensions,
17+
},
18+
});
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ApolloServerErrorCode } from '@apollo/server/errors';
2+
import { GraphQLError, GraphQLErrorOptions } from 'graphql';
3+
4+
/**
5+
* This error is thrown when the user input does not pass validation.
6+
*
7+
* "ValidationError" class was removed in the latest version of Apollo Server (4.0.0)
8+
* It was moved to the @nestjs/apollo package to avoid regressions & make migration easier.
9+
*/
10+
export class ValidationError extends GraphQLError {
11+
constructor(message: string, options?: GraphQLErrorOptions) {
12+
super(message, {
13+
...options,
14+
extensions: {
15+
code: ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED,
16+
...options?.extensions,
17+
},
18+
});
19+
}
20+
}

packages/apollo/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './decorators';
22
export * from './drivers';
3+
export * from './errors';
34
export * from './interfaces';
45
export * from './utils';

0 commit comments

Comments
 (0)