File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { GraphQLError } from 'graphql';
55import { PRESERVED_STATUS_CODES } from './exception.constant' ;
66import {
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+
8796const 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 : {
Original file line number Diff line number Diff line change @@ -27,3 +27,25 @@ export const isHttpException = (error: unknown): error is HttpException => {
2727export 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+ } ;
You can’t perform that action at this time.
0 commit comments