@@ -79,6 +79,19 @@ export async function defaultHandler(
79
79
headers ,
80
80
queryString === null ? undefined : queryString ,
81
81
) ;
82
+ // We return a 400 here if imageParams returns an errorMessage
83
+ // https://github.com/vercel/next.js/blob/512d8283054407ab92b2583ecce3b253c3be7b85/packages/next/src/server/next-server.ts#L937-L941
84
+ if ( "errorMessage" in imageParams ) {
85
+ error (
86
+ "Error during validation of image params" ,
87
+ imageParams . errorMessage ,
88
+ ) ;
89
+ return buildFailureResponse (
90
+ imageParams . errorMessage ,
91
+ options ?. streamCreator ,
92
+ 400 ,
93
+ ) ;
94
+ }
82
95
let etag : string | undefined ;
83
96
// We don't cache any images, so in order to be able to return 304 responses, we compute an ETag from what is assumed to be static
84
97
if ( process . env . OPENNEXT_STATIC_ETAG ) {
@@ -99,10 +112,13 @@ export async function defaultHandler(
99
112
nextConfig ,
100
113
downloadHandler ,
101
114
) ;
102
-
103
115
return buildSuccessResponse ( result , options ?. streamCreator , etag ) ;
104
116
} catch ( e : any ) {
105
- return buildFailureResponse ( e , options ?. streamCreator ) ;
117
+ error ( "Failed to optimize image" , e ) ;
118
+ return buildFailureResponse (
119
+ "Internal server error" ,
120
+ options ?. streamCreator ,
121
+ ) ;
106
122
}
107
123
}
108
124
@@ -124,9 +140,6 @@ function validateImageParams(
124
140
false ,
125
141
) ;
126
142
debug ( "image params" , imageParams ) ;
127
- if ( "errorMessage" in imageParams ) {
128
- throw new Error ( imageParams . errorMessage ) ;
129
- }
130
143
return imageParams ;
131
144
}
132
145
@@ -182,34 +195,33 @@ function buildSuccessResponse(
182
195
}
183
196
184
197
function buildFailureResponse (
185
- e : any ,
198
+ errorMessage : string ,
186
199
streamCreator ?: StreamCreator ,
200
+ statusCode = 500 ,
187
201
) : InternalResult {
188
- debug ( e ) ;
202
+ debug ( errorMessage , statusCode ) ;
189
203
if ( streamCreator ) {
190
204
const response = new OpenNextNodeResponse (
191
205
( ) => void 0 ,
192
206
async ( ) => void 0 ,
193
207
streamCreator ,
194
208
) ;
195
- response . writeHead ( 500 , {
209
+ response . writeHead ( statusCode , {
196
210
Vary : "Accept" ,
197
211
"Cache-Control" : "public,max-age=60,immutable" ,
198
- "Content-Type" : "application/json" ,
199
212
} ) ;
200
- response . end ( e ?. message || e ?. toString ( ) || "An error occurred" ) ;
213
+ response . end ( errorMessage ) ;
201
214
}
202
215
return {
203
216
type : "core" ,
204
217
isBase64Encoded : false ,
205
- statusCode : 500 ,
218
+ statusCode : statusCode ,
206
219
headers : {
207
220
Vary : "Accept" ,
208
221
// For failed images, allow client to retry after 1 minute.
209
222
"Cache-Control" : "public,max-age=60,immutable" ,
210
- "Content-Type" : "application/json" ,
211
223
} ,
212
- body : toReadableStream ( e ?. message || e ?. toString ( ) || "An error occurred" ) ,
224
+ body : toReadableStream ( errorMessage ) ,
213
225
} ;
214
226
}
215
227
0 commit comments