Hexilee/roa

roa::logger::logger日志错误状态码记录不正确

shaitao opened this issue · 3 comments

use std::sync::Arc;
use roa::{App, Context, Status};
use roa::preload::*;
use roa::router::Router;
use roa::http::StatusCode;

struct MyState {
    inner: i64,
}

#[derive(Clone)]
struct SharedState(Arc<MyState>);

async fn info(ctx: &mut Context<SharedState>) -> roa::Result {
    let inner = ctx.0.inner;
    ctx.write(format!("Hello, {}", inner));
    Err(Status::new(StatusCode::INTERNAL_SERVER_ERROR, "This is an error",true))
}

#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::init();
    let router = Router::new().on("/info", info);
    let state = SharedState(Arc::new(MyState { inner: 0 }));
    let app = App::new(state)
        .gate(roa::logger::logger)
        .end(router.routes("/")?);

    app.listen("0.0.0.0:9999", |addr| {
        println!("Start server on {:?}", addr);
    })?
        .await?;
    Ok(())
}

浏览器访问确实拿到了500, 日志里面记录了200
[2020-04-01T02:34:29Z ERROR roa::logger] <-- GET /info 0ms 200 OK
This is an error

看了下源码发现是 roa::Context.resp.status和roa::Status.status_code不一致导致的

谢谢,这个 bug 应该在最新版本 v0.5.0-rc.6 中被修复了

如果没有其它问题,我可以关了这个 issue 吗?

可以关了, 暂时没发现问题