我试图理解错误从Actix的回购协议examples的一个处理。它使用failure
箱子来处理错误。下面是相关的代码:
#[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())
}
}
如果我的处理程序返回ServiceError
代码不慌,它会呈现一个HttpResponse
(见error_response()
)。正因为如此,我就不能在我的终端看到Fail
消息(#[fail(display
...)。
有没有加入到println!
在error_response
比其他我的日志中显示任何好的内置的方式?我认为这完全是有道理的,以显示确切的错误,而不是一般的InternalServerError
:即NetworkError / ParseError。
如果没有,什么是设计时没有看到确切的错误的能力,原因是什么?
Actix的的Web呈现错误log::error!
。尝试使用RUST_LOG=actix_web=debug
开始你的榜样