Struct poem::middleware::CatchPanic
source · pub struct CatchPanic<H> { /* private fields */ }
Expand description
Middleware for catches panics and converts them into 500 INTERNAL SERVER ERROR
responses.
§Example
use http::StatusCode;
use poem::{handler, middleware::CatchPanic, test::TestClient, EndpointExt, Route};
#[handler]
async fn index() {
panic!()
}
let app = Route::new().at("/", index).with(CatchPanic::new());
let cli = TestClient::new(app);
let resp = cli.get("/").send().await;
resp.assert_status(StatusCode::INTERNAL_SERVER_ERROR);
Implementations§
source§impl<H> CatchPanic<H>
impl<H> CatchPanic<H>
sourcepub fn with_handler<T: PanicHandler>(self, handler: T) -> CatchPanic<T>
pub fn with_handler<T: PanicHandler>(self, handler: T) -> CatchPanic<T>
Specifies a panic handler to be used to create a custom response when a panic occurs.
§Example
use http::StatusCode;
use poem::{
handler, middleware::CatchPanic, test::TestClient, EndpointExt, IntoResponse, Route,
};
#[handler]
async fn index() {
panic!()
}
let app = Route::new().at("/", index).with(
CatchPanic::new().with_handler(|_| "error!".with_status(StatusCode::INTERNAL_SERVER_ERROR)),
);
let cli = TestClient::new(app);
let resp = cli.get("/").send().await;
resp.assert_status(StatusCode::INTERNAL_SERVER_ERROR);
resp.assert_text("error!").await;
Trait Implementations§
source§impl Default for CatchPanic<()>
impl Default for CatchPanic<()>
source§impl<E: Endpoint, H: PanicHandler> Middleware<E> for CatchPanic<H>
impl<E: Endpoint, H: PanicHandler> Middleware<E> for CatchPanic<H>
Auto Trait Implementations§
impl<H> Freeze for CatchPanic<H>where
H: Freeze,
impl<H> RefUnwindSafe for CatchPanic<H>where
H: RefUnwindSafe,
impl<H> Send for CatchPanic<H>where
H: Send,
impl<H> Sync for CatchPanic<H>where
H: Sync,
impl<H> Unpin for CatchPanic<H>where
H: Unpin,
impl<H> UnwindSafe for CatchPanic<H>where
H: UnwindSafe,
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
Available on crate feature
tower-compat
only.Converts a tower service to a poem endpoint.