Skip to content

Commit

Permalink
Do not shutdown on request panic. Fix returned error format in body.
Browse files Browse the repository at this point in the history
  • Loading branch information
piohei committed Dec 13, 2024
1 parent 4af5d40 commit e30f1f9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/server/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl IntoResponse for Error {
let status_code = self.to_status_code();

let body = if let Self::Other(err) = self {
format!("{err:?}")
format!("{err}")
} else {
self.to_string()
};
Expand Down
15 changes: 2 additions & 13 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ pub async fn run(app: Arc<App>, config: ServerConfig, shutdown: Shutdown) -> any
}

#[derive(Clone)]
struct PanicHandler {
shutdown: Shutdown,
}
struct PanicHandler {}

impl ResponseForPanic for PanicHandler {
type ResponseBody = Body;
Expand All @@ -154,20 +152,13 @@ impl ResponseForPanic for PanicHandler {
error: Box<dyn std::any::Any + Send + 'static>,
) -> hyper::Response<Self::ResponseBody> {
tracing::error!(?error, "request panicked");
self.shutdown.clone().shutdown();
hyper::Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
.unwrap()
}
}

impl From<Shutdown> for PanicHandler {
fn from(shutdown: Shutdown) -> Self {
Self { shutdown }
}
}

/// # Errors
///
/// Will return `Err` if the provided `listener` address cannot be accessed or
Expand All @@ -194,9 +185,7 @@ pub async fn bind_from_listener(
.layer(middleware::from_fn(
custom_middleware::api_metrics_layer::middleware,
))
.layer(CatchPanicLayer::custom(PanicHandler {
shutdown: shutdown.clone(),
}))
.layer(CatchPanicLayer::custom(PanicHandler {}))
.layer(middleware::from_fn_with_state(
serve_timeout,
custom_middleware::timeout_layer::middleware,
Expand Down

0 comments on commit e30f1f9

Please sign in to comment.