我怎样才能打印Actix的的Web错误的消息,而无需恐慌?

问题描述 投票:1回答:1

我试图理解错误从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。

如果没有,什么是设计时没有看到确切的错误的能力,原因是什么?

rust rust-actix
1个回答
0
投票

Actix的的Web呈现错误log::error!。尝试使用RUST_LOG=actix_web=debug开始你的榜样

© www.soinside.com 2019 - 2024. All rights reserved.