diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/handler.rs | 10 | ||||
| -rw-r--r-- | src/router.rs | 15 |
2 files changed, 17 insertions, 8 deletions
diff --git a/src/handler.rs b/src/handler.rs index f22e3b3..b283cdf 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -30,8 +30,14 @@ pub trait RouteResponse: impl<T> RouteResponse for T where T: FnMut(RouteContext<'_>) -> Response + Send + Sync {} -pub type ErrorResponse = - Box<dyn FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync>; +pub trait ErrorResponse: + FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync +{ +} + +impl<T> ErrorResponse for T where T: FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync +{} + pub type Callback = Box<dyn FnMut(CallbackContext<'_>) + Send + Sync>; pub type CleanupCallback = Box<dyn FnMut(CallbackContext<'_>, &mut Response) + Send + Sync>; diff --git a/src/router.rs b/src/router.rs index 9917923..0f4af89 100644 --- a/src/router.rs +++ b/src/router.rs @@ -57,7 +57,7 @@ macro_rules! or_error { #[derive(Clone)] pub struct Router { routes: matchit::Router<Arc<Mutex<Box<dyn RouteResponse>>>>, - error_handler: Arc<Mutex<ErrorResponse>>, + error_handler: Arc<Mutex<Box<dyn ErrorResponse>>>, private_key_file_name: String, ca_file_name: String, headers: Arc<Mutex<Vec<Partial>>>, @@ -151,12 +151,15 @@ impl Router { /// # Examples /// /// ```rust - /// windmark::Router::new().set_error_handler(Box::new(|_| { - /// windmark::Response::success("You have encountered an error!") - /// })); + /// windmark::Router::new().set_error_handler(|_| { + /// windmark::success!("You have encountered an error!") + /// }); /// ``` - pub fn set_error_handler(&mut self, handler: ErrorResponse) -> &mut Self { - self.error_handler = Arc::new(Mutex::new(handler)); + pub fn set_error_handler( + &mut self, + handler: impl ErrorResponse + 'static, + ) -> &mut Self { + self.error_handler = Arc::new(Mutex::new(Box::new(handler))); self } |