use std::panic::AssertUnwindSafe;

use futures::FutureExt;

use axum::{
    extract::Request,
    middleware::Next,
    response::{IntoResponse, Response},
};

use crate::status::api_err::ApiErr;

pub async fn catch_mw(req: Request, next: Next) -> Response {
    let ret = AssertUnwindSafe(next.run(req)).catch_unwind().await;
    if let Ok(resp) = ret {
        return resp;
    }
    ApiErr::Panic.into_response()
}
