Skip to content

Commit

Permalink
Add CatchPanic (#214)
Browse files Browse the repository at this point in the history
* Add `CatchPanic`

* support custom panic handler

* docs

* fix feature

* Add note about using panics for error handling
  • Loading branch information
davidpdrsn committed Mar 5, 2022
1 parent d9955de commit bcc5470
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 1 deletion.
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

0 comments on commit bcc5470

Please sign in to comment.