Skip to content

Commit

Permalink
Update to have types and use/export them
Browse files Browse the repository at this point in the history
  • Loading branch information
brycefranzen committed Apr 13, 2022
1 parent 9e94d05 commit bfbaae1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
42 changes: 24 additions & 18 deletions packages/apollo-server-errors/src/ApolloServerErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
printError,
formatError,
} from 'graphql';
import { ErrorCode, ErrorName } from './types';

declare module 'graphql' {
export interface GraphQLErrorExtensions {
Expand Down Expand Up @@ -40,7 +41,7 @@ export class ApolloError extends Error implements GraphQLError {

// if no name provided, use the default. defineProperty ensures that it stays non-enumerable
if (!this.name) {
Object.defineProperty(this, 'name', { value: 'ApolloError' });
Object.defineProperty(this, 'name', { value: ErrorName.APOLLO_ERROR });
}

if (extensions?.extensions) {
Expand Down Expand Up @@ -115,7 +116,7 @@ function enrichError(error: Partial<GraphQLError>, debug: boolean = false) {

expanded.extensions = {
...error.extensions,
code: error.extensions?.code || 'INTERNAL_SERVER_ERROR',
code: error.extensions?.code || ErrorCode.INTERNAL_SERVER_ERROR,
exception: {
...error.extensions?.exception,
...(error.originalError as any),
Expand All @@ -141,7 +142,7 @@ function enrichError(error: Partial<GraphQLError>, debug: boolean = false) {

export function toApolloError(
error: Error & { extensions?: Record<string, any> },
code: string = 'INTERNAL_SERVER_ERROR',
code: string = ErrorCode.INTERNAL_SERVER_ERROR,
): Error & { extensions: Record<string, any> } {
let err = error;
if (err.extensions) {
Expand Down Expand Up @@ -180,7 +181,7 @@ export function fromGraphQLError(error: GraphQLError, options?: ErrorOptions) {

// Fallback on default for code
if (!copy.extensions.code) {
copy.extensions.code = options?.code || 'INTERNAL_SERVER_ERROR';
copy.extensions.code = options?.code || ErrorCode.INTERNAL_SERVER_ERROR;
}

// copy the original error, while keeping all values non-enumerable, so they
Expand All @@ -197,61 +198,66 @@ export function fromGraphQLError(error: GraphQLError, options?: ErrorOptions) {

export class SyntaxError extends ApolloError {
constructor(message: string) {
super(message, 'GRAPHQL_PARSE_FAILED');
super(message, ErrorCode.GRAPHQL_PARSE_FAILED);

Object.defineProperty(this, 'name', { value: 'SyntaxError' });
Object.defineProperty(this, 'name', { value: ErrorName.SYNTAX_ERROR });
}
}

export class ValidationError extends ApolloError {
constructor(message: string) {
super(message, 'GRAPHQL_VALIDATION_FAILED');
super(message, ErrorCode.GRAPHQL_VALIDATION_FAILED);

Object.defineProperty(this, 'name', { value: 'ValidationError' });
Object.defineProperty(this, 'name', { value: ErrorName.VALIDATION_ERROR });
}
}

export class AuthenticationError extends ApolloError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'UNAUTHENTICATED', extensions);
super(message, ErrorCode.UNAUTHENTICATED, extensions);

Object.defineProperty(this, 'name', { value: 'AuthenticationError' });
Object.defineProperty(this, 'name', {
value: ErrorName.AUTHENTICATION_ERROR,
});
}
}

export class ForbiddenError extends ApolloError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'FORBIDDEN', extensions);
super(message, ErrorCode.FORBIDDEN, extensions);

Object.defineProperty(this, 'name', { value: 'ForbiddenError' });
Object.defineProperty(this, 'name', { value: ErrorName.FORBIDDEN_ERROR });
}
}

export class PersistedQueryNotFoundError extends ApolloError {
constructor() {
super('PersistedQueryNotFound', 'PERSISTED_QUERY_NOT_FOUND');
super('PersistedQueryNotFound', ErrorCode.PERSISTED_QUERY_NOT_FOUND);

Object.defineProperty(this, 'name', {
value: 'PersistedQueryNotFoundError',
value: ErrorName.PERSISTED_QUERY_NOT_FOUND_ERROR,
});
}
}

export class PersistedQueryNotSupportedError extends ApolloError {
constructor() {
super('PersistedQueryNotSupported', 'PERSISTED_QUERY_NOT_SUPPORTED');
super(
'PersistedQueryNotSupported',
ErrorCode.PERSISTED_QUERY_NOT_SUPPORTED,
);

Object.defineProperty(this, 'name', {
value: 'PersistedQueryNotSupportedError',
value: ErrorName.PERSISTED_QUERY_NOT_SUPPORTED_ERROR,
});
}
}

export class UserInputError extends ApolloError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'BAD_USER_INPUT', extensions);
super(message, ErrorCode.BAD_USER_INPUT, extensions);

Object.defineProperty(this, 'name', { value: 'UserInputError' });
Object.defineProperty(this, 'name', { value: ErrorName.USER_INPUT_ERROR });
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ApolloServerErrors';
export * from './types';
73 changes: 73 additions & 0 deletions packages/apollo-server-errors/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
export enum ErrorCode {
/**
* The GraphQL operation string contains a syntax error.
*/
GRAPHQL_PARSE_FAILED = 'GRAPHQL_PARSE_FAILED',
/**
* The GraphQL operation is not valid against the server's schema.
*/
GRAPHQL_VALIDATION_FAILED = 'GRAPHQL_VALIDATION_FAILED',
/**
* The GraphQL operation includes an invalid value for a field argument.
*/
BAD_USER_INPUT = 'BAD_USER_INPUT',
/**
* The server failed to authenticate with a required data source, such as a REST API.
*/
UNAUTHENTICATED = 'UNAUTHENTICATED',
/**
* The server was unauthorized to access a required data source, such as a REST API.
*/
FORBIDDEN = 'FORBIDDEN',
/**
* A client sent the hash of a query string to execute via automatic persisted queries, but the query was not in the APQ cache.
*/
PERSISTED_QUERY_NOT_FOUND = 'PERSISTED_QUERY_NOT_FOUND',
/**
* A client sent the hash of a query string to execute via automatic persisted queries, but the server has disabled APQ.
*/
PERSISTED_QUERY_NOT_SUPPORTED = 'PERSISTED_QUERY_NOT_SUPPORTED',
/**
* This is the default error code returned by any ApolloError instance that doesn't specify a different code.
*/
INTERNAL_SERVER_ERROR = 'INTERNAL_SERVER_ERROR',
}

export type TErrorCode = `${ErrorCode}`;

export enum ErrorName {
/**
* A error occurred without a name provided. This is the default name.
*/
APOLLO_ERROR = 'ApolloError',
/**
* The GraphQL operation string contains a syntax error.
*/
SYNTAX_ERROR = 'SyntaxError',
/**
* The GraphQL operation is not valid against the server's schema.
*/
VALIDATION_ERROR = 'ValidationError',
/**
* The server failed to authenticate with a required data source, such as a REST API.
*/
AUTHENTICATION_ERROR = 'AuthenticationError',
/**
* The server was unauthorized to access a required data source, such as a REST API.
*/
FORBIDDEN_ERROR = 'ForbiddenError',
/**
* A client sent the hash of a query string to execute via automatic persisted queries, but the query was not in the APQ cache.
*/
PERSISTED_QUERY_NOT_FOUND_ERROR = 'PersistedQueryNotFoundError',
/**
* A client sent the hash of a query string to execute via automatic persisted queries, but the server has disabled APQ.
*/
PERSISTED_QUERY_NOT_SUPPORTED_ERROR = 'PersistedQueryNotSupportedError',
/**
* The GraphQL operation includes an invalid value for a field argument.
*/
USER_INPUT_ERROR = 'UserInputError',
}

export type TErrorName = `${ErrorName}`;

0 comments on commit bfbaae1

Please sign in to comment.