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?