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

Add CatchPanic #214

Merged
merged 6 commits into from
Feb 22, 2022
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
3 changes: 2 additions & 1 deletion tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Added

- None.
- Added `CatchPanic` middleware which catches panics and converts them
into `500 Internal Server` responses

## Changed

Expand Down
3 changes: 3 additions & 0 deletions tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ tokio = { version = "1", features = ["full"] }
tower = { version = "0.4.10", features = ["buffer", "util", "retry", "make", "timeout"] }
tracing-subscriber = "0.3"
uuid = { version = "0.8", features = ["v4"] }
serde_json = "1.0"

[features]
default = []
full = [
"add-extension",
"auth",
"catch-panic",
"compression-full",
"cors",
"decompression-full",
Expand All @@ -73,6 +75,7 @@ full = [

add-extension = []
auth = ["base64"]
catch-panic = ["tracing", "futures-util/std"]
cors = []
follow-redirect = ["iri-string", "tower/util"]
fs = ["tokio/fs", "tokio-util/io", "tokio/io-util", "mime_guess", "mime", "percent-encoding", "httpdate"]
Expand Down
23 changes: 23 additions & 0 deletions tower-http/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,19 @@ pub trait ServiceBuilderExt<L>: crate::sealed::Sealed<L> + Sized {
) -> ServiceBuilder<Stack<crate::request_id::PropagateRequestIdLayer, L>> {
self.propagate_request_id(HeaderName::from_static(crate::request_id::X_REQUEST_ID))
}

/// Catch panics and convert them into `500 Internal Server` responses.
///
/// See [`tower_http::catch_panic`] for more details.
///
/// [`tower_http::catch_panic`]: crate::catch_panic
#[cfg(feature = "catch-panic")]
#[cfg_attr(docsrs, doc(cfg(feature = "catch-panic")))]
fn catch_panic(
self,
) -> ServiceBuilder<
Stack<crate::catch_panic::CatchPanicLayer<crate::catch_panic::DefaultResponseForPanic>, L>,
>;
}

impl<L> crate::sealed::Sealed<L> for ServiceBuilder<L> {}
Expand Down Expand Up @@ -588,4 +601,14 @@ impl<L> ServiceBuilderExt<L> for ServiceBuilder<L> {
) -> ServiceBuilder<Stack<crate::request_id::PropagateRequestIdLayer, L>> {
self.layer(crate::request_id::PropagateRequestIdLayer::new(header_name))
}

#[cfg(feature = "catch-panic")]
#[cfg_attr(docsrs, doc(cfg(feature = "catch-panic")))]
fn catch_panic(
self,
) -> ServiceBuilder<
Stack<crate::catch_panic::CatchPanicLayer<crate::catch_panic::DefaultResponseForPanic>, L>,
> {
self.layer(crate::catch_panic::CatchPanicLayer::new())
}
}
Loading