@@ -23,23 +23,27 @@ export class APIError<
2323 readonly request_id : string | null | undefined ;
2424
2525 constructor ( status : TStatus , error : TError , message : string | undefined , headers : THeaders ) {
26- super ( `${ APIError . makeMessage ( status , error , message ) } ` ) ;
26+ // Always format message when constructing
27+ const formattedMessage = APIError . makeMessage ( status , error , message ) ;
28+ super ( formattedMessage ) ; // Use formatted message directly
2729 this . status = status ;
2830 this . headers = headers ;
2931 this . request_id = headers ?. [ 'x-request-id' ] ;
30- this . error = error ;
32+ this . error = formattedMessage ;
3133
32- const data = error as Record < string , any > ;
33- this . code = data ?. [ 'code' ] ;
34- this . param = data ?. [ 'param' ] ;
35- this . type = data ?. [ 'type' ] ;
34+ // You can still retain the raw error object for later use, but store the formatted message
35+ this . code = error ?. [ 'code' ] ;
36+ this . param = error ?. [ 'param' ] ;
37+ this . type = error ?. [ 'type' ] ;
3638 }
3739
3840 private static makeMessage ( status : number | undefined , error : any , message : string | undefined ) {
3941 const msg =
4042 error ?. message ?
4143 typeof error . message === 'string' ?
4244 error . message
45+ . replace ( / ' / g, '"' ) // Convert single quotes to double quotes
46+ . replace ( / \( \s * ( [ ^ , ] + ) , \s * ( [ ^ , ] + ) \s * \) / g, '[$1, $2]' ) // Convert tuples to arrays (e.g., ('body', 'input') -> ["body", "input"])
4347 : JSON . stringify ( error . message )
4448 : error ? JSON . stringify ( error )
4549 : message ;
@@ -100,6 +104,7 @@ export class APIError<
100104 return new InternalServerError ( status , error , message , headers ) ;
101105 }
102106
107+ // Default to a generic APIError if no specific handling
103108 return new APIError ( status , error , message , headers ) ;
104109 }
105110}
0 commit comments