@@ -37,6 +37,7 @@ thread_local! {
3737pub struct HttpRequestContext {
3838 pub request : IncomingHttpRequest ,
3939 pub response_headers : HashMap < String , String > ,
40+ pub response_status : http:: StatusCode ,
4041}
4142
4243pub struct AppContext {
@@ -95,6 +96,15 @@ pub fn add_response_header(key: String, value: String) {
9596 } )
9697}
9798
99+ // Set the HTTP response status code
100+ pub fn set_response_status ( status : http:: StatusCode ) {
101+ APP_HELPERS . with ( |helpers| {
102+ if let Some ( ctx) = & mut helpers. borrow_mut ( ) . current_http_context {
103+ ctx. response_status = status;
104+ }
105+ } )
106+ }
107+
98108pub fn clear_http_request_context ( ) {
99109 APP_HELPERS . with ( |helpers| {
100110 helpers. borrow_mut ( ) . current_http_context = None ;
@@ -267,9 +277,16 @@ where
267277 return Ok ( r) ;
268278 }
269279
270- let e = serde_json:: from_slice :: < SendError > ( & response_bytes)
271- . expect ( "Failed to deserialize response to send()" ) ;
272- return Err ( AppSendError :: SendError ( e) ) ;
280+ match serde_json:: from_slice :: < SendError > ( & response_bytes) {
281+ Ok ( e) => Err ( AppSendError :: SendError ( e) ) ,
282+ Err ( err) => {
283+ error ! (
284+ "Failed to deserialize response in send(): {} (payload: {:?})" ,
285+ err, response_bytes
286+ ) ;
287+ Err ( AppSendError :: BuildError ( BuildError :: NoBody ) )
288+ }
289+ }
273290}
274291
275292pub async fn send_rmp < R > ( request : Request ) -> Result < R , AppSendError >
@@ -292,9 +309,16 @@ where
292309 return Ok ( r) ;
293310 }
294311
295- let e = rmp_serde:: from_slice :: < SendError > ( & response_bytes)
296- . expect ( "Failed to deserialize response to send()" ) ;
297- return Err ( AppSendError :: SendError ( e) ) ;
312+ match rmp_serde:: from_slice :: < SendError > ( & response_bytes) {
313+ Ok ( e) => Err ( AppSendError :: SendError ( e) ) ,
314+ Err ( err) => {
315+ error ! (
316+ "Failed to deserialize response in send_rmp(): {} (payload: {:?})" ,
317+ err, response_bytes
318+ ) ;
319+ Err ( AppSendError :: BuildError ( BuildError :: NoBody ) )
320+ }
321+ }
298322}
299323
300324// Enum defining the state persistance behaviour
0 commit comments