Skip to content

Commit 07f670c

Browse files
committed
fix: add HttpException handling in GraphQL error formatter
1 parent 5a0c02d commit 07f670c

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/common/exceptions/exception.constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const GRAPHQL_ERROR_CODES = [
1010
];
1111

1212
export const PRESERVED_STATUS_CODES = [
13+
HttpStatus.BAD_REQUEST,
1314
HttpStatus.UNAUTHORIZED,
1415
HttpStatus.FORBIDDEN,
1516
] as const;

src/common/exceptions/exception.format.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GraphQLError } from 'graphql';
44

55
import { PRESERVED_STATUS_CODES } from './exception.constant';
66
import {
7+
getHttpExceptionCode,
78
isBaseException,
89
isGraphqlOriginalError,
910
isHttpException,
@@ -26,6 +27,14 @@ const determineErrorCondition = (error: GraphQLError) => {
2627
};
2728
}
2829

30+
if (isHttpException(error.originalError)) {
31+
const status = error.originalError.getStatus();
32+
return {
33+
errorStatus: status,
34+
errorCode: getHttpExceptionCode(status),
35+
};
36+
}
37+
2938
return {
3039
errorStatus: HttpStatus.INTERNAL_SERVER_ERROR,
3140
errorCode: 'INTERNAL_SERVER_ERROR',

src/common/exceptions/exception.util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpException } from '@nestjs/common';
1+
import { HttpException, HttpStatus } from '@nestjs/common';
22

33
import { GraphQLErrorExtensions } from 'graphql';
44

@@ -23,3 +23,7 @@ export const isBaseException = (
2323
export const isHttpException = (error: unknown): error is HttpException => {
2424
return error instanceof HttpException;
2525
};
26+
27+
export const getHttpExceptionCode = (status: number): string => {
28+
return HttpStatus[status] || 'HTTP_ERROR';
29+
};

0 commit comments

Comments
 (0)