@@ -189,7 +189,19 @@ async function handleWebSocketWithDispatcher(
189189 serverWs . accept ( ) ;
190190
191191 // Forward the upgrade request to the container
192- const containerResponse = await container . fetch ( request ) ;
192+ let containerResponse : Response ;
193+ try {
194+ containerResponse = await container . fetch ( request ) ;
195+ } catch ( error ) {
196+ // Container connection failed - close client WebSocket with error
197+ const errorMsg = error instanceof Error ? error . message : 'Container connection failed' ;
198+ console . error ( 'Container fetch failed during WebSocket upgrade:' , errorMsg ) ;
199+ serverWs . close ( 1011 , `Container unreachable: ${ errorMsg } ` ) ;
200+ return new Response ( 'Service Unavailable: Container connection failed' , {
201+ status : 503 ,
202+ statusText : 'Container Unreachable' ,
203+ } ) ;
204+ }
193205
194206 if ( containerResponse . status !== 101 || ! containerResponse . webSocket ) {
195207 // Container didn't upgrade - return error to client
@@ -308,7 +320,20 @@ export default {
308320
309321 // Start the container and wait for ports to be ready
310322 // This is required for the fetch to work properly
311- await container . startAndWaitForPorts ( ) ;
323+ try {
324+ await container . startAndWaitForPorts ( ) ;
325+ } catch ( error ) {
326+ const errorMsg = error instanceof Error ? error . message : 'Container failed to start' ;
327+ const errorStack = error instanceof Error ? error . stack : undefined ;
328+ console . error ( 'Container failed to start:' , errorMsg , errorStack || '' ) ;
329+ return new Response ( `Service Unavailable: Container failed to start (${ errorMsg } )` , {
330+ status : 503 ,
331+ statusText : 'Container Start Failed' ,
332+ headers : {
333+ 'Content-Type' : 'text/plain' ,
334+ } ,
335+ } ) ;
336+ }
312337
313338 // Report connection tracking for autoscale mode
314339 const routingMode = env . ROUTING_MODE || 'session' ;
@@ -336,7 +361,29 @@ export default {
336361 }
337362
338363 // Forward request directly to container (pass-through mode)
339- return container . fetch ( request ) ;
364+ try {
365+ return await container . fetch ( request ) ;
366+ } catch ( error ) {
367+ // Container connection failed
368+ const errorMsg = error instanceof Error ? error . message : 'Container connection failed' ;
369+ const errorStack = error instanceof Error ? error . stack : undefined ;
370+ console . error ( 'Container fetch failed:' , errorMsg , errorStack || '' ) ;
371+
372+ // Return appropriate error response
373+ const isWebSocket = upgradeHeader === 'websocket' ;
374+ return new Response (
375+ isWebSocket
376+ ? `WebSocket Upgrade Failed: Container unreachable (${ errorMsg } )`
377+ : `Service Unavailable: Container connection failed (${ errorMsg } )` ,
378+ {
379+ status : 503 ,
380+ statusText : 'Container Unreachable' ,
381+ headers : {
382+ 'Content-Type' : 'text/plain' ,
383+ } ,
384+ } ,
385+ ) ;
386+ }
340387
341388 } ,
342389} satisfies ExportedHandler < Env > ;
0 commit comments