3

I'm trying to understand the error handling from one of the examples from the Actix repo. It uses the failure crate to handle errors. Here's a relevant piece of code:

#[derive(Fail, Debug)]
pub enum ServiceError {
    #[fail(display = "Internal Server Error: {}", _0)]
    InternalServerError(String),

    #[fail(display = "BadRequest: {}", _0)]
    BadRequest(String),

    #[fail(display = "Unauthorized")]
    Unauthorized,
}

impl ResponseError for ServiceError {
    fn error_response(&self) -> HttpResponse {
        match *self {
            ServiceError::InternalServerError { .. } => HttpResponse::InternalServerError().json("Internal Server Error, Please try later"),
            ServiceError::BadRequest(ref message) => HttpResponse::BadRequest().json(message)
        }
    }
}

impl From<ParseError> for ServiceError {
    fn from(_: ParseError) -> ServiceError {
        ServiceError::BadRequest("Invalid UUID".into())
    }
}

If my handler returns a ServiceError the code doesn't panic, it will render an HttpResponse (see error_response()). Because of this, I won't be able to see a Fail message (#[fail(display...) in my terminal.

Is there any nice built-in way to display it in my logs other than by adding println! to error_response? I believe it totally makes sense to display the exact error rather than generic InternalServerError: i.e., NetworkError/ParseError.

If not, what is the reason it was designed without the ability to see the exact error?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
James Larkin
  • 541
  • 5
  • 18

1 Answers1

1

Actix-Web renders errors to log::error!. Try to start your example with RUST_LOG=actix_web=debug

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nikolay Kim
  • 144
  • 1
  • Could you specify the former part of your answer? – James Larkin Feb 04 '19 at 22:31
  • 1
    @JamesLarkin `actix` logs all errors using the `log` crate. Depending on the [logger your program uses](https://docs.rs/log/0.4.6/log/index.html#available-logging-implementations), some messages will be filtered out. Nikolay assumes you are using [`env_logger`](https://docs.rs/env_logger/*/env_logger/) or something compatible, which allows to change the filter while starting the program to include those error messages. – mcarton Feb 04 '19 at 22:37