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
sourceimpl CatchPanic<()>
impl CatchPanic<()>
sourceimpl<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
sourceimpl Default for CatchPanic<()>
impl Default for CatchPanic<()>
sourceimpl<E: Endpoint, H: PanicHandler> Middleware<E> for CatchPanic<H>
impl<E: Endpoint, H: PanicHandler> Middleware<E> for CatchPanic<H>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more