rs_express/middleware/
error_middleware.rs

1use crate::middleware::matchable_middleware::{
2    MatchableMethod, MatchableMiddleware, MatchablePath,
3};
4use crate::middleware::NextFunction;
5use crate::{Error, Request, Response};
6
7pub struct ErrorMiddleware<T> {
8    pub(crate) base_url: &'static str,
9    func: Box<
10        dyn Fn(&Error, &mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
11    >,
12}
13
14impl<T> ErrorMiddleware<T> {
15    pub fn new(
16        base_url: &'static str,
17        func: Box<
18            dyn Fn(&Error, &mut Request<T>, &mut Response, &mut NextFunction)
19                + Send
20                + Sync
21                + 'static,
22        >,
23    ) -> ErrorMiddleware<T> {
24        ErrorMiddleware { base_url, func }
25    }
26
27    pub fn invoke(
28        &self,
29        error: &Error,
30        req: &mut Request<T>,
31        res: &mut Response,
32        next: &mut NextFunction,
33    ) {
34        (self.func)(error, req, res, next);
35    }
36}
37
38impl<T> MatchableMiddleware<T> for ErrorMiddleware<T> {
39    fn path(&self) -> MatchablePath {
40        MatchablePath::All
41    }
42
43    fn method(&self) -> MatchableMethod {
44        MatchableMethod::All
45    }
46}