我有下面的代码,一切正常,期待error_handler
. 我不确定有什么问题。如果我以正确的格式发出发布请求,我会得到 200 响应数据。但是在糟糕的请求下,我期望 400 没有接收到 JSON 数据。它只是说 400,但不返回任何错误 json 数据。
预期行为
错误请求 ->curl -X POST -H "Content-Type: application/json" -d '{"bad": "Someone was here"}' localhost:8080/send
status 400
{
error: String,
server_id: usize,
request_count: usize,
}
当前行为
错误请求 ->curl -X POST -H "Content-Type: application/json" -d '{"bad": "Someone was here"}' localhost:8080/send
status 400
可能的解决方案
我用它替换.data
它.app_data
可以工作,但后来它无法找到AppState
代码
pub struct MessageApp {
pub port: u16,
}
impl MessageApp {
pub fn new(port: u16) -> Self {
Self{port}
}
pub async fn run(&self) -> std::io::Result<()> {
let messages = Arc::new(Mutex::new(vec![]));
println!("Starting http server: 127.0.0.1:{}", self.port);
HttpServer::new(move || {
App::new()
.data(AppState {
server_id: SERVER_COUNTER.fetch_add(1, Ordering::SeqCst),
request_count: Cell::new(0),
messages: messages.clone(),
})
.wrap(middleware::Logger::new(LOG_FORMAT))
.service(index)
.service(
web::resource("/send")
.data(
web::JsonConfig::default()
.limit(4096)
.error_handler(post_error)
)
.route(web::post().to(post))
)
.service(clear)
})
.bind(("127.0.0.1", self.port))?
.workers(8)
.run()
.await
}
}
错误处理程序
fn post_error(err: JsonPayloadError, req: &HttpRequest) -> Error {
let extns = req.extensions();
let state = extns.get::<web::Data<AppState>>().unwrap();
let request_count = state.request_count.get() + 1;
state.request_count.set(request_count);
let post_error = PostError {
server_id: state.server_id,
request_count,
error: format!("{}", err),
};
InternalError::from_response(err, HttpResponse::BadRequest().json(post_error)).into()
}
环境
- 流行操作系统 20.04 LTS
- Rust 版本:rustc 1.45.2 (d3fb005a3 2020-07-31)
- Actix 网页版:2.0.0