Skip to content

Commit

Permalink
Add http status to graphql errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thomtrp committed Jun 17, 2024
1 parent d8034b1 commit 404212f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ export const shouldFilterException = (exception: Error): boolean => {
) {
return true;
}

if (exception instanceof HttpException && exception.getStatus() < 500) {
return true;
}

return false;
};

export const handleException = (
const handleException = (
exception: Error,
exceptionHandlerService: ExceptionHandlerService,
user?: ExceptionHandlerUser,
Expand All @@ -72,7 +73,7 @@ export const convertExceptionToGraphQLError = (
return convertExceptionToGraphql(exception);
};

export const convertHttpExceptionToGraphql = (exception: HttpException) => {
const convertHttpExceptionToGraphql = (exception: HttpException) => {
const status = exception.getStatus();
let error: BaseGraphQLError;

Expand Down
19 changes: 11 additions & 8 deletions packages/twenty-server/src/engine/utils/graphql-errors.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare module 'graphql' {
}
}

export class BaseGraphQLError extends Error implements GraphQLError {
export class BaseGraphQLError extends GraphQLError {
public extensions: Record<string, any>;
override readonly name!: string;
readonly locations: ReadonlyArray<SourceLocation> | undefined;
Expand Down Expand Up @@ -84,23 +84,23 @@ export class SyntaxError extends BaseGraphQLError {

export class ValidationError extends BaseGraphQLError {
constructor(message: string) {
super(message, 'GRAPHQL_VALIDATION_FAILED');
super(message, 'GRAPHQL_VALIDATION_FAILED', { http: { status: 400 } });

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

export class AuthenticationError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'UNAUTHENTICATED', extensions);
super(message, 'UNAUTHENTICATED', { ...extensions, http: { status: 401 } });

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

export class ForbiddenError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'FORBIDDEN', extensions);
super(message, 'FORBIDDEN', { ...extensions, http: { status: 403 } });

Object.defineProperty(this, 'name', { value: 'ForbiddenError' });
}
Expand Down Expand Up @@ -136,31 +136,34 @@ export class UserInputError extends BaseGraphQLError {

export class NotFoundError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'NOT_FOUND', extensions);
super(message, 'NOT_FOUND', { ...extensions, http: { status: 404 } });

Object.defineProperty(this, 'name', { value: 'NotFoundError' });
}
}

export class MethodNotAllowedError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'METHOD_NOT_ALLOWED', extensions);
super(message, 'METHOD_NOT_ALLOWED', {
...extensions,
http: { status: 405 },
});

Object.defineProperty(this, 'name', { value: 'MethodNotAllowedError' });
}
}

export class ConflictError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'CONFLICT', extensions);
super(message, 'CONFLICT', { ...extensions, http: { status: 409 } });

Object.defineProperty(this, 'name', { value: 'ConflictError' });
}
}

export class TimeoutError extends BaseGraphQLError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, 'TIMEOUT', extensions);
super(message, 'TIMEOUT', { ...extensions, http: { status: 408 } });

Object.defineProperty(this, 'name', { value: 'TimeoutError' });
}
Expand Down

0 comments on commit 404212f

Please sign in to comment.