use super::middleware::final_error_layer;
use axum::{
Router,
middleware::from_fn,
};
use tower_http::catch_panic::CatchPanicLayer;
#[cfg(feature = "tera")]
use super::middleware::graceful_error_layer;
#[cfg(feature = "tera")]
use crate::app::state::StateProvider as AppStateProvider;
#[cfg(feature = "tera")]
use ::{
axum::middleware::from_fn_with_state,
std::sync::Arc,
};
pub trait RouterExt<S>
where
S: Clone + Send + Sync + 'static,
{
#[must_use]
fn add_error_catcher(self) -> Self;
#[cfg(feature = "tera")]
#[must_use]
fn add_error_template<SP>(self, state: &Arc<SP>) -> Self
where
SP: AppStateProvider,
;
}
impl<S> RouterExt<S> for Router<S>
where
S: Clone + Send + Sync + 'static,
{
fn add_error_catcher(self) -> Self {
self
.layer(CatchPanicLayer::new())
.layer(from_fn(final_error_layer))
}
#[cfg(feature = "tera")]
fn add_error_template<SP>(self, state: &Arc<SP>) -> Self
where
SP: AppStateProvider,
{
self
.layer(CatchPanicLayer::new())
.layer(from_fn_with_state(Arc::clone(state), graceful_error_layer))
}
}