Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not shutdown on request panic. Fix returned error format in body. #834

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading