How to send a proper Error response in actix_web middleware?

I develop an authentication middleware for actix_web. I can send an OK response just fine but I cannot send an error message. Here's what I've done so far using the example given here:

/// ResponseBody is my own custom struct to send an error/content body
#[derive(Debug, Serialize, Deserialize)]
pub struct ResponseBody<T> {
   pub message: String,
   pub data: T,
}

impl<T> ResponseBody<T> {
   pub fn new(message: &str, data: T) -> ResponseBody<T> {
       ResponseBody {
           message: message.to_string(),
           data,
       }
   }
}

/// ....somewhere in my middleware
/// if user passes then I will send him to the destination 
/// this code was copied from https://actix.rs/docs/middleware/ example
if authenticate_pass {
        let fut = self.service.call(req);
        Box::pin(async move {
            let res = fut.await?;
            Ok(res)
        })

/// but if he fails to signin or for some other reasons
/// I want to send an error response UNAUTHORIZED (401)
    } else {
        Box::pin(async move {
            Ok(req.into_response(
                HttpResponse::Unauthorized()
                    .json(ResponseBody::new("Session ends. Please login again", ""))
                    .into_body(),
            ))
        })
    }

It displays an error message that I should implement a conversion from actix_web::dev::Response into BoxBody. I don't have any idea what BoxBody is and how to implement them.

error[E0277]: the trait bound `actix_web::dev::Response<_>: std::convert::From<BoxBody>` is not satisfied
   --> src\auth.rs:103:21
    |
102 |                   Ok(req.into_response(
    |                          ------------- required by a bound introduced by this call
103 | /                     HttpResponse::Unauthorized()
104 | |                         .json(ResponseBody::new("Session ends. Please login again", ""))
105 | |                         .into_body(),
    | |____________________________________^ the trait `std::convert::From<BoxBody>` is not implemented for `actix_web::dev::Response<_>`
    |
    = help: the following other types implement trait `std::convert::From<T>`:
              <actix_web::dev::Response<&'static [u8]> as std::convert::From<&'static [u8]>>
              <actix_web::dev::Response<&'static str> as std::convert::From<&'static str>>
              <actix_web::dev::Response<B> as std::convert::From<HttpResponse<B>>>
              <actix_web::dev::Response<B> as std::convert::From<ServiceResponse<B>>>
              <actix_web::dev::Response<BoxBody> as std::convert::From<&actix_http::ws::HandshakeError>>
              <actix_web::dev::Response<BoxBody> as std::convert::From<HttpResponseBuilder>>
              <actix_web::dev::Response<BoxBody> as std::convert::From<Infallible>>
              <actix_web::dev::Response<BoxBody> as std::convert::From<Result<I, E>>>
            and 13 others
    = note: required because of the requirements on the impl of `Into<actix_web::dev::Response<_>>` for `BoxBody`
note: required by a bound in `ServiceRequest::into_response`
   --> C:\Users\mdenn\.cargo\registry\src\github.com-1ecc6299db9ec823\actix-web-4.1.0\src\service.rs:144:32
    |
144 |     pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
    |                                ^^^^^^^^^^^^^^^^^ required by this bound in `ServiceRequest::into_response`

If anyone have experience with actix_web middleware, please help. Thanks in advance.

FULL CODE: pastebin

actix-web = "4.1.0"
futures = "0.3.24"
tokio = { version = "1.18.2", features = ["full"] }
serde = { version = "1.0.137", features = ["derive"] }

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.