use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;
use std::sync::Arc;
#[async_trait::async_trait]
pub trait ExceptionFilter: Send + Sync {
async fn catch(&self, ex: crate::HttpException) -> Response;
}
pub(crate) async fn exception_filter_middleware(
axum::extract::State(filter): axum::extract::State<Arc<dyn ExceptionFilter>>,
req: Request,
next: Next,
) -> Response {
let res = next.run(req).await;
if let Some(ex) = res.extensions().get::<crate::HttpException>().cloned() {
filter.catch(ex).await
} else {
res
}
}