@@ -131,12 +131,12 @@ export async function serverResponse(req: Request, body: string): Promise<Respon
131131
132132  if  ( ! foundRoute )  { 
133133    // 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>' ,  { 
135135      status : 404 , 
136136      headers : { 
137137        'Access-Control-Allow-Origin' : '*' , 
138138        'Access-Control-Allow-Headers' : '*' , 
139-         'Content-Type' : 'application/json ' , 
139+         'Content-Type' : 'text/html ' , 
140140      } , 
141141    } ) 
142142  } 
@@ -190,91 +190,130 @@ function extractDynamicSegments(routePattern: string, path: string): RouteParam
190190} 
191191
192192async  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 }  ,  { 
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 }  ,  { 
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 ,  { 
203240      headers : { 
204-         'Content-Type' :  'application/json' , 
241+         ... headers , 
205242        'Access-Control-Allow-Origin' : '*' , 
206243        'Access-Control-Allow-Headers' : '*' , 
207244      } , 
208-       status :  status   ||   401 , 
245+       status, 
209246    } ) 
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 , 
215251      headers : { 
252+         'Content-Type' : 'text/html' , 
216253        'Access-Control-Allow-Origin' : '*' , 
217254        'Access-Control-Allow-Headers' : '*' , 
218255      } , 
219256    } ) 
220257  } 
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-   } ) 
234258} 
235259
236260async  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 }  ) 
244-     const  requestImport  =  await  import ( requestPath ) 
245-     const  requestInstance  =  requestImport [ `${ camelCase ( modelName ) }  ] 
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 }  ) 
270+         const  requestImport  =  await  import ( requestPath ) 
271+         const  requestInstance  =  requestImport [ `${ camelCase ( modelName ) }  ] 
272+ 
273+         if  ( requestInstance )  { 
274+           requestInstance [ operation ] ( data ) 
275+         } 
276+       }  catch  ( error )  { 
277+         log . error ( `Error processing model file ${ modelFile } ${ error }  ) 
278+         continue 
279+       } 
249280    } 
250-   } 
251281
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 ) }  ] 
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 ) }  ] 
258288
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 
261295      } 
262296    } 
263-     catch  ( error )  { 
264-       log . error ( `Error importing trait interface: ${ error }  ) 
265-       continue 
266-     } 
267-   } 
268297
269-   RequestParam [ operation ] ( data ) 
298+     RequestParam [ operation ] ( data ) 
299+   }  catch  ( error )  { 
300+     log . error ( `Error in applyToAllRequests: ${ error }  ) 
301+   } 
270302} 
271303
272304async  function  addRouteQuery ( url : URL ) : Promise < void >  { 
273305  await  applyToAllRequests ( 'addQuery' ,  url ) 
274306} 
275307
276308async  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+   } 
278317} 
279318
280319async  function  addRouteParam ( param : RouteParam ) : Promise < void >  { 
0 commit comments