Skip to content

Commit 404212f

Browse files
committed
Add http status to graphql errors
1 parent d8034b1 commit 404212f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/twenty-server/src/engine/utils/global-exception-handler.util.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ export const shouldFilterException = (exception: Error): boolean => {
4343
) {
4444
return true;
4545
}
46+
4647
if (exception instanceof HttpException && exception.getStatus() < 500) {
4748
return true;
4849
}
4950

5051
return false;
5152
};
5253

53-
export const handleException = (
54+
const handleException = (
5455
exception: Error,
5556
exceptionHandlerService: ExceptionHandlerService,
5657
user?: ExceptionHandlerUser,
@@ -72,7 +73,7 @@ export const convertExceptionToGraphQLError = (
7273
return convertExceptionToGraphql(exception);
7374
};
7475

75-
export const convertHttpExceptionToGraphql = (exception: HttpException) => {
76+
const convertHttpExceptionToGraphql = (exception: HttpException) => {
7677
const status = exception.getStatus();
7778
let error: BaseGraphQLError;
7879

packages/twenty-server/src/engine/utils/graphql-errors.util.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ declare module 'graphql' {
1515
}
1616
}
1717

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

8585
export class ValidationError extends BaseGraphQLError {
8686
constructor(message: string) {
87-
super(message, 'GRAPHQL_VALIDATION_FAILED');
87+
super(message, 'GRAPHQL_VALIDATION_FAILED', { http: { status: 400 } });
8888

8989
Object.defineProperty(this, 'name', { value: 'ValidationError' });
9090
}
9191
}
9292

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

9797
Object.defineProperty(this, 'name', { value: 'AuthenticationError' });
9898
}
9999
}
100100

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

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

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

141141
Object.defineProperty(this, 'name', { value: 'NotFoundError' });
142142
}
143143
}
144144

145145
export class MethodNotAllowedError extends BaseGraphQLError {
146146
constructor(message: string, extensions?: Record<string, any>) {
147-
super(message, 'METHOD_NOT_ALLOWED', extensions);
147+
super(message, 'METHOD_NOT_ALLOWED', {
148+
...extensions,
149+
http: { status: 405 },
150+
});
148151

149152
Object.defineProperty(this, 'name', { value: 'MethodNotAllowedError' });
150153
}
151154
}
152155

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

157160
Object.defineProperty(this, 'name', { value: 'ConflictError' });
158161
}
159162
}
160163

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

165168
Object.defineProperty(this, 'name', { value: 'TimeoutError' });
166169
}

0 commit comments

Comments
 (0)