Skip to content

Commit 7a7f7b1

Browse files
committed
fix: extract actual validation message from HttpException
1 parent 07f670c commit 7a7f7b1

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/common/exceptions/exception.format.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GraphQLError } from 'graphql';
55
import { PRESERVED_STATUS_CODES } from './exception.constant';
66
import {
77
getHttpExceptionCode,
8+
getHttpExceptionMessage,
89
isBaseException,
910
isGraphqlOriginalError,
1011
isHttpException,
@@ -84,6 +85,14 @@ const handleInternalServerError = (
8485
}
8586
};
8687

88+
const determineErrorMessage = (error: GraphQLError): string => {
89+
if (isHttpException(error.originalError)) {
90+
return getHttpExceptionMessage(error.originalError);
91+
}
92+
93+
return error.message;
94+
};
95+
8796
const formatError = (
8897
error: GraphQLError,
8998
options: {
@@ -104,7 +113,7 @@ const formatError = (
104113
options.setHttpStatus(httpStatus);
105114

106115
return {
107-
message: error.message,
116+
message: determineErrorMessage(error),
108117
locations: error.locations,
109118
path: error.path,
110119
extensions: {

src/common/exceptions/exception.util.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,25 @@ export const isHttpException = (error: unknown): error is HttpException => {
2727
export const getHttpExceptionCode = (status: number): string => {
2828
return HttpStatus[status] || 'HTTP_ERROR';
2929
};
30+
31+
export const getHttpExceptionMessage = (error: HttpException): string => {
32+
const response = error.getResponse();
33+
34+
if (typeof response === 'string') {
35+
return response;
36+
}
37+
38+
if (typeof response === 'object' && response !== null) {
39+
const { message } = response as { message?: string | string[] };
40+
41+
if (Array.isArray(message)) {
42+
return message.join(', ');
43+
}
44+
45+
if (typeof message === 'string') {
46+
return message;
47+
}
48+
}
49+
50+
return error.message;
51+
};

0 commit comments

Comments
 (0)