How to return err with wrap_fn middleware in actix-web

Hi all,
'm trying to return an response err in a function middleware,
how should I define the return value type

use actix_web::{
    dev::{Service, ServiceRequest},
    http::header::AUTHORIZATION,
};

pub async fn verify_auth<S>(req: ServiceRequest, srv: &S) -> S::Future
where
    S: Service<ServiceRequest>,
{
    let auth = req.headers().get(AUTHORIZATION);

    match auth {
        Some(token) => srv.call(req),
        None => {
        
            //if none return a response err
        }
    }
}

App::wrap_fn takes middleware functions as input that return Future<Output = Result<ServiceResponse<B>, Error>> + Clone + 'static, where Error is actix_web::error::Error.

You can convert any type that implements actix_web::error::ResponseError to actix_web::error::Error. You could write your own error type and implement ResponseError for that or use a type that already implements this trait. I personally use actix_web_httpauth::extractors::AuthenticationError for failures in my authentication middleware.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.