@@ -131,12 +131,12 @@ export async function serverResponse(req: Request, body: string): Promise<Respon
131
131
132
132
if ( ! foundRoute ) {
133
133
// TODO: create a pretty 404 page
134
- return new Response ( '<html><body><h1>Route not found!</h1<pre></pre ></body></html>' , {
134
+ return new Response ( '<html><body><h1>Route not found!</h1></body></html>' , {
135
135
status : 404 ,
136
136
headers : {
137
137
'Access-Control-Allow-Origin' : '*' ,
138
138
'Access-Control-Allow-Headers' : '*' ,
139
- 'Content-Type' : 'application/json ' ,
139
+ 'Content-Type' : 'text/html ' ,
140
140
} ,
141
141
} )
142
142
}
@@ -190,91 +190,130 @@ function extractDynamicSegments(routePattern: string, path: string): RouteParam
190
190
}
191
191
192
192
async function execute ( foundRoute : Route , req : Request , _options : Options ) {
193
- const foundCallback = await route . resolveCallback ( foundRoute . callback )
194
-
195
- const middlewarePayload = await executeMiddleware ( foundRoute )
196
- if (
197
- middlewarePayload !== null
198
- && typeof middlewarePayload === 'object'
199
- && Object . keys ( middlewarePayload ) . length > 0
200
- ) {
201
- const { status, message } = middlewarePayload
202
- return new Response ( `<html><body><h1>${ message } </h1></body></html>` , {
193
+ try {
194
+ const foundCallback = await route . resolveCallback ( foundRoute . callback )
195
+
196
+ if ( ! foundCallback ) {
197
+ return new Response ( '<html><body><h1>Route callback not found!</h1></body></html>' , {
198
+ status : 500 ,
199
+ headers : {
200
+ 'Content-Type' : 'text/html' ,
201
+ 'Access-Control-Allow-Origin' : '*' ,
202
+ 'Access-Control-Allow-Headers' : '*' ,
203
+ } ,
204
+ } )
205
+ }
206
+
207
+ const middlewarePayload = await executeMiddleware ( foundRoute )
208
+ if (
209
+ middlewarePayload !== null
210
+ && typeof middlewarePayload === 'object'
211
+ && Object . keys ( middlewarePayload ) . length > 0
212
+ ) {
213
+ const { status, message } = middlewarePayload
214
+ return new Response ( `<html><body><h1>${ message } </h1></body></html>` , {
215
+ headers : {
216
+ 'Content-Type' : 'text/html' ,
217
+ 'Access-Control-Allow-Origin' : '*' ,
218
+ 'Access-Control-Allow-Headers' : '*' ,
219
+ } ,
220
+ status : status || 401 ,
221
+ } )
222
+ }
223
+
224
+ if ( foundRoute ?. method !== req . method ) {
225
+ return new Response ( '<html><body><h1>Method not allowed!</h1></body></html>' , {
226
+ status : 405 ,
227
+ headers : {
228
+ 'Content-Type' : 'text/html' ,
229
+ 'Access-Control-Allow-Origin' : '*' ,
230
+ 'Access-Control-Allow-Headers' : '*' ,
231
+ } ,
232
+ } )
233
+ }
234
+
235
+ // foundCallback is now a ResponseData object from response.ts
236
+ const { status, headers, body } = foundCallback
237
+
238
+ // Return the response with the exact body from response.ts
239
+ return new Response ( body , {
203
240
headers : {
204
- 'Content-Type' : 'application/json' ,
241
+ ... headers ,
205
242
'Access-Control-Allow-Origin' : '*' ,
206
243
'Access-Control-Allow-Headers' : '*' ,
207
244
} ,
208
- status : status || 401 ,
245
+ status,
209
246
} )
210
- }
211
-
212
- if ( foundRoute ?. method !== req . method ) {
213
- return new Response ( '<html><body><h1>Method not allowed!</h1></body></html>' , {
214
- status : 405 ,
247
+ } catch ( error : any ) {
248
+ log . error ( `Error executing route: ${ error . message } ` )
249
+ return new Response ( '<html><body><h1>Internal Server Error</h1></body></html>' , {
250
+ status : 500 ,
215
251
headers : {
252
+ 'Content-Type' : 'text/html' ,
216
253
'Access-Control-Allow-Origin' : '*' ,
217
254
'Access-Control-Allow-Headers' : '*' ,
218
255
} ,
219
256
} )
220
257
}
221
-
222
- // foundCallback is now a ResponseData object from response.ts
223
- const { status, headers, body } = foundCallback
224
-
225
- // Return the response with the exact body from response.ts
226
- return new Response ( body , {
227
- headers : {
228
- ...headers ,
229
- 'Access-Control-Allow-Origin' : '*' ,
230
- 'Access-Control-Allow-Headers' : '*' ,
231
- } ,
232
- status,
233
- } )
234
258
}
235
259
236
260
async function applyToAllRequests ( operation : 'addBodies' | 'addParam' | 'addHeaders' | 'addQuery' , data : any ) : Promise < void > {
237
- const modelFiles = globSync ( [ path . userModelsPath ( '*.ts' ) , path . storagePath ( 'framework/defaults/models/**/*.ts' ) ] , { absolute : true } )
238
-
239
- // Process model files
240
- for ( const modelFile of modelFiles ) {
241
- const model = ( await import ( modelFile ) ) . default as Model
242
- const modelName = getModelName ( model , modelFile )
243
- const requestPath = path . frameworkPath ( `requests/${ modelName } Request.ts` )
244
- const requestImport = await import ( requestPath )
245
- const requestInstance = requestImport [ `${ camelCase ( modelName ) } Request` ]
246
-
247
- if ( requestInstance ) {
248
- requestInstance [ operation ] ( data )
261
+ try {
262
+ const modelFiles = globSync ( [ path . userModelsPath ( '*.ts' ) , path . storagePath ( 'framework/defaults/models/**/*.ts' ) ] , { absolute : true } )
263
+
264
+ // Process model files
265
+ for ( const modelFile of modelFiles ) {
266
+ try {
267
+ const model = ( await import ( modelFile ) ) . default as Model
268
+ const modelName = getModelName ( model , modelFile )
269
+ const requestPath = path . frameworkPath ( `requests/${ modelName } Request.ts` )
270
+ const requestImport = await import ( requestPath )
271
+ const requestInstance = requestImport [ `${ camelCase ( modelName ) } Request` ]
272
+
273
+ if ( requestInstance ) {
274
+ requestInstance [ operation ] ( data )
275
+ }
276
+ } catch ( error ) {
277
+ log . error ( `Error processing model file ${ modelFile } : ${ error } ` )
278
+ continue
279
+ }
249
280
}
250
- }
251
281
252
- // Process trait interfaces
253
- for ( const trait of traitInterfaces ) {
254
- const requestPath = path . frameworkPath ( `requests/ ${ trait . name } Request.ts` )
255
- try {
256
- const requestImport = await import ( requestPath )
257
- const requestInstance = requestImport [ `${ camelCase ( trait . name ) } Request` ]
282
+ // Process trait interfaces
283
+ for ( const trait of traitInterfaces ) {
284
+ try {
285
+ const requestPath = path . frameworkPath ( `requests/ ${ trait . name } Request.ts` )
286
+ const requestImport = await import ( requestPath )
287
+ const requestInstance = requestImport [ `${ camelCase ( trait . name ) } Request` ]
258
288
259
- if ( requestInstance ) {
260
- requestInstance [ operation ] ( data )
289
+ if ( requestInstance ) {
290
+ requestInstance [ operation ] ( data )
291
+ }
292
+ } catch ( error ) {
293
+ log . error ( `Error importing trait interface: ${ error } ` )
294
+ continue
261
295
}
262
296
}
263
- catch ( error ) {
264
- log . error ( `Error importing trait interface: ${ error } ` )
265
- continue
266
- }
267
- }
268
297
269
- RequestParam [ operation ] ( data )
298
+ RequestParam [ operation ] ( data )
299
+ } catch ( error ) {
300
+ log . error ( `Error in applyToAllRequests: ${ error } ` )
301
+ }
270
302
}
271
303
272
304
async function addRouteQuery ( url : URL ) : Promise < void > {
273
305
await applyToAllRequests ( 'addQuery' , url )
274
306
}
275
307
276
308
async function addBody ( params : any ) : Promise < void > {
277
- await applyToAllRequests ( 'addBodies' , JSON . parse ( params ) )
309
+ try {
310
+ const parsedParams = typeof params === 'string' ? JSON . parse ( params ) : params
311
+ await applyToAllRequests ( 'addBodies' , parsedParams )
312
+ } catch ( error ) {
313
+ log . error ( `Error parsing request body: ${ error } ` )
314
+ // Continue with empty object if parsing fails
315
+ await applyToAllRequests ( 'addBodies' , { } )
316
+ }
278
317
}
279
318
280
319
async function addRouteParam ( param : RouteParam ) : Promise < void > {
0 commit comments