@@ -596,12 +596,7 @@ impl ExecuteCtx {
596596 "There was an error handling the request {}" ,
597597 e. to_string( )
598598 ) ;
599- #[ allow( unused_mut) ]
600- let mut response = Response :: builder ( )
601- . status ( hyper:: StatusCode :: INTERNAL_SERVER_ERROR )
602- . body ( Body :: empty ( ) )
603- . unwrap ( ) ;
604- ( response, Some ( e) )
599+ ( anyhow_response ( & e) , Some ( e) )
605600 }
606601 Err ( e) => panic ! ( "failed to run guest: {}" , e) ,
607602 }
@@ -678,7 +673,15 @@ impl ExecuteCtx {
678673
679674 // Ensure the downstream response channel is closed, whether or not a response was
680675 // sent during execution.
681- store. data_mut ( ) . session . close_downstream_response_sender ( ) ;
676+ let resp = outcome
677+ . as_ref ( )
678+ . err ( )
679+ . map ( exec_err_to_response)
680+ . unwrap_or_default ( ) ;
681+ store
682+ . data_mut ( )
683+ . session
684+ . close_downstream_response_sender ( resp) ;
682685
683686 let request_duration = Instant :: now ( ) . duration_since ( start_timestamp) ;
684687
@@ -745,7 +748,12 @@ impl ExecuteCtx {
745748
746749 // Ensure the downstream response channel is closed, whether or not a response was
747750 // sent during execution.
748- store. data_mut ( ) . close_downstream_response_sender ( ) ;
751+ let resp = outcome
752+ . as_ref ( )
753+ . err ( )
754+ . map ( exec_err_to_response)
755+ . unwrap_or_default ( ) ;
756+ store. data_mut ( ) . close_downstream_response_sender ( resp) ;
749757
750758 let request_duration = Instant :: now ( ) . duration_since ( start_timestamp) ;
751759
@@ -830,7 +838,9 @@ impl ExecuteCtx {
830838
831839 // Ensure the downstream response channel is closed, whether or not a response was
832840 // sent during execution.
833- store. data_mut ( ) . close_downstream_response_sender ( ) ;
841+ store
842+ . data_mut ( )
843+ . close_downstream_response_sender ( Response :: default ( ) ) ;
834844
835845 // We don't do anything with any response on the receiver, but
836846 // it's important to keep it alive until after the program has
@@ -986,17 +996,24 @@ fn write_profile(store: &mut wasmtime::Store<WasmCtx>, guest_profile_path: Optio
986996}
987997
988998fn guest_result_to_response ( resp : Response < Body > , err : Option < anyhow:: Error > ) -> Response < Body > {
989- if let Some ( err) = err {
990- let body = err . root_cause ( ) . to_string ( ) ;
991- Response :: builder ( )
992- . status ( hyper :: StatusCode :: INTERNAL_SERVER_ERROR )
993- . body ( Body :: from ( body . as_bytes ( ) ) )
994- . unwrap ( )
999+ err. as_ref ( ) . map ( anyhow_response ) . unwrap_or ( resp )
1000+ }
1001+
1002+ fn exec_err_to_response ( err : & ExecutionError ) -> Response < Body > {
1003+ if let ExecutionError :: WasmTrap ( e ) = err {
1004+ anyhow_response ( e )
9951005 } else {
996- resp
1006+ panic ! ( "failed to run guest: {err}" )
9971007 }
9981008}
9991009
1010+ fn anyhow_response ( err : & anyhow:: Error ) -> Response < Body > {
1011+ Response :: builder ( )
1012+ . status ( hyper:: StatusCode :: INTERNAL_SERVER_ERROR )
1013+ . body ( Body :: from ( format ! ( "{err:?}" ) . into_bytes ( ) ) )
1014+ . unwrap ( )
1015+ }
1016+
10001017impl Drop for ExecuteCtx {
10011018 fn drop ( & mut self ) {
10021019 if let Some ( join_handle) = self . epoch_increment_thread . take ( ) {
0 commit comments