@@ -26,6 +26,8 @@ export type FileUploadContext = {
26
26
fileName : string ;
27
27
} ;
28
28
29
+ export type CustomErrorMessage = string | ( ( event : APIGatewayProxyEventV2 ) => string ) ;
30
+
29
31
export const withLargeResponseHandler = ( {
30
32
thresholdWarn,
31
33
thresholdError,
@@ -38,7 +40,7 @@ export const withLargeResponseHandler = ({
38
40
thresholdError : number ;
39
41
sizeLimitInMB : number ;
40
42
outputBucket : string ;
41
- customErrorMessage ?: string | ( ( event : APIGatewayProxyEventV2 ) => string ) ;
43
+ customErrorMessage ?: CustomErrorMessage ;
42
44
groupRequestsBy ?: ( event : APIGatewayProxyEventV2 ) => string ;
43
45
} ) => {
44
46
return {
@@ -105,12 +107,8 @@ export const withLargeResponseHandler = ({
105
107
} ) ;
106
108
response . isBase64Encoded = false ;
107
109
response . statusCode = 413 ;
108
- const responseErrorMessage = customErrorMessage ?? LARGE_RESPONSE_USER_INFO ;
109
-
110
110
response . body = JSON . stringify ( {
111
- message : typeof responseErrorMessage === 'string'
112
- ? responseErrorMessage
113
- : customErrorMessage ?.( event ) || customErrorMessage ,
111
+ message : getCustomErrorMessage ( customErrorMessage , event ) ,
114
112
} ) ;
115
113
}
116
114
} else if ( contentLengthMB > thresholdWarnInMB ) {
@@ -200,3 +198,18 @@ function getFormattedDate() {
200
198
201
199
return date . toISOString ( ) . split ( 'T' ) [ 0 ] ;
202
200
}
201
+
202
+ function getCustomErrorMessage ( customErrorMessage : CustomErrorMessage | undefined , event : APIGatewayProxyEventV2 ) {
203
+ let message ;
204
+
205
+ if ( typeof customErrorMessage === 'string' ) {
206
+ message = customErrorMessage ;
207
+ } else if ( typeof customErrorMessage === 'function' ) {
208
+ message = customErrorMessage ( event ) ;
209
+ } else {
210
+ // If customErrorMessage is neither a string nor a function or is not defined, use a fallback.
211
+ message = customErrorMessage ?? LARGE_RESPONSE_USER_INFO ;
212
+ }
213
+
214
+ return message ;
215
+ }
0 commit comments