From 642504f2ba2ac8147654a0b379bef1e12ef124f2 Mon Sep 17 00:00:00 2001 From: Alex Lazar Date: Fri, 29 Mar 2024 00:37:12 -0700 Subject: [PATCH] Remove panic for axum ResponseOptions (#2468) --- integrations/axum/src/lib.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/integrations/axum/src/lib.rs b/integrations/axum/src/lib.rs index df6301b898..6286f70bc4 100644 --- a/integrations/axum/src/lib.rs +++ b/integrations/axum/src/lib.rs @@ -302,12 +302,6 @@ async fn handle_server_fns_inner( // actually run the server fn let mut res = service.run(req).await; - // update response as needed - let res_options = expect_context::().0; - let res_options_inner = res_options.read(); - let (status, mut res_headers) = - (res_options_inner.status, res_options_inner.headers.clone()); - // if it accepts text/html (i.e., is a plain form post) and doesn't already have a // Location set, then redirect to Referer if accepts_html { @@ -320,11 +314,22 @@ async fn handle_server_fns_inner( } } - // apply status code and headers if used changed them - if let Some(status) = status { - *res.status_mut() = status; + // update response as needed + if let Some(res_options) = use_context::() { + let res_options_inner = res_options.0.read(); + let (status, mut res_headers) = ( + res_options_inner.status, + res_options_inner.headers.clone(), + ); + + // apply status code and headers if used changed them + if let Some(status) = status { + *res.status_mut() = status; + } + res.headers_mut().extend(res_headers.drain()); + } else { + eprintln!("Failed to find ResponseOptions for {path}"); } - res.headers_mut().extend(res_headers.drain()); // clean up the scope runtime.dispose();