- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 actix_web 中有一个异步处理程序,如果没有设置多个 header ,它一定会失败。我不明白在返回 Future
的函数中处理错误的最佳方式应该是什么。我基本上想要一个等价于 ?
操作符的 future 。
这是我当前的代码:
r.post().with_async(
move |req: HttpRequest, path: Path<EventPath>, body: Json<EventCreationRequest>| {
let headers = req.headers();
let client_id = match headers
.get("x-client-id")
.ok_or("Header not found")
.and_then(|v| v.to_str().map_err(|_| "Invalid header content"))
{
Err(e) => return ok(HttpResponse::BadRequest().body(e)).responder(),
Ok(v) => v.to_string(),
};
operation_that_returns_future()
.map(|_| HttpResponse::Ok().body("OK!"))
.responder()
},
);
我通过匹配提前返回解决了 future 缺少 ?
运算符的问题。但是,在我的代码中,我实际上需要确保存在一堆其他 header 。
理想情况下,我想将匹配和早期返回逻辑提取到可重用的东西中,但在这种情况下,这迫使我创建一个宏。这似乎有点矫枉过正,特别是如果语言中已经有一些东西可以让我做我想做的事。
处理这种情况最惯用的方法是什么?
最佳答案
要处理错误,返回失败的 Future
。例如,将 header 检查作为 Future
,然后将您的 futures 与 .and_then
链接起来。一个技巧是保持 futures 的错误类型相同,以避免 map_err
。例如:
fn handler(req: HttpRequest) -> impl Future<Item = HttpResponse, Error = Error> {
has_client_header(&req)
.and_then(|client| operation_that_returns_future(client))
.map(|result| HttpResponse::Ok().body(result))
}
fn has_client_header(req: &HttpRequest) -> impl Future<Item = String, Error = Error> {
if let Some(Ok(client)) = req.headers().get("x-client-id").map(|h| h.to_str()) {
future::ok(client.to_owned())
} else {
future::failed(ErrorBadRequest("invalid x-client-id header"))
}
}
fn operation_that_returns_future(client: String) -> impl Future<Item = String, Error = Error> {
future::ok(client)
}
结果:
$ curl localhost:8000
invalid x-client-id header⏎
$ curl localhost:8000 -H 'x-client-id: asdf'
asdf⏎
当 operation_that_returns_future
有另一种错误类型时:
fn handler(req: HttpRequest) -> impl Future<Item = HttpResponse, Error = Error> {
has_client_header(&req)
.and_then(|client| {
operation_that_returns_future(client)
.map_err(|_| ErrorInternalServerError("operation failed"))
})
.map(|result| HttpResponse::Ok().body(result))
}
另一个技巧是使用 failure crate ,它提供将所有错误映射到一种类型 failure::Error
的 failure::Error::from
。
最后,您可能会找到 actix_web::guards
用于检查 header 值:
.guard(guard::Header("x-client-id", "special client"))
关于rust - 在 actix-web 中处理异步处理程序中的错误的最惯用的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417315/
因此,我正在尝试创建一个基本的actix-web应用程序,该应用程序将允许我创建一个非常基本的博客系统。它正在处理我的GET请求,但没有处理我的POST请求。 main.rs: use actix_w
我对Rust还是很陌生,并且愿意为Actix-web中的Orange Pi Zero提供一些Linux服务,该服务将充当其他网络设备的“网关”(一些愚蠢的中文网络中继,具有4个输入和4个输出,这是受控
您好,我想传递以下 AppState。 pub struct AppState { clients: Vec, } 这就是我的服务器: async fn launch_server(app_c
我正在尝试为我的 Actix 应用程序编写身份验证中间件。在中间件中验证请求时,我调用数据库以检索必要的用户数据以验证传入的请求。一旦请求获得授权,我希望能够将此用户数据传递给处理程序,因为这将允许我
我只是在玩一些Actix网络和Actix Actor ,而我在构建一个简单的应用程序却花了毫秒的时间,所以我观察到Actix正在创建多个 Actor 。我只想限制它一个,但我做不到。 我在这里做错了什
主要功能是: #[actix_rt::main] async fn main() -> std::io::Result { std::env::set_var( "RUST_L
我想为 actix 中的查询值设置默认值。 我知道有一个Default Rust 标准库中结构的特征,但老实说,我不知道如何在这种情况下应用它。 在我的情况下,请求查询可能会也可能不会提供分页值页面和
我正在尝试运行两个应用程序(一个在端口 3006 上进行管理,另一个在端口 8080 上提供数据)。 他们共享数据库池、缓存... 对于actix 1.0,我有这个工作(我不知道这是否是最好的方法):
这是我第一次尝试使用 actix-web 编写带有 Rust 的小型 Web 服务。 下面的代码是一个请求处理程序,旨在做三件事,在数据库中插入一个条目,如果数据库调用成功则发送一封电子邮件,然后返回
我正在尝试使用 SyncArbiter 实现一个包含 10 个 Redis 连接的池,供不同的参与者使用。假设我们有一个名为 Bob 的 actor,它必须使用 Redis actor 来完成它的任务
我正在尝试创建具有 PyO3 Python 解释器和 Py 对象的 Actix Actor。 问题是创建 python 解释器 actor 的正确方法是什么? 我认为错误是由静态定义的 Actor 特
我正在使用 Actix-web 实现中间件,但遇到了一个我无法弄清楚的生命周期问题。 extern crate actix_web; use actix_web::actix::{Actor, Add
我正在尝试使用 actix-web 1.0 编写 HTTP 端点。我已经缩减了函数,使其只返回传递给它的用户,但编译器仍然给出错误。 extern crate actix_web; extern cr
问题 - 如果您只有一个系统但其中有多个仲裁器在运行,它仍然是一个单线程事件循环吗? 通读 Actix 书 - https://actix.rs/book/actix/sec-6-sync-arbit
我现在有以下服务器声明 let server = HttpServer::new(move || { App::new() .app_data(actix_web::web::
我在使用波纹管代码在actix-web上使用Stream时遇到问题: fn format_csv_row(row: tiberius::Row) -> Result { ... } #[get("/s
我试过 example of actix-multipart与 actix-web v3.3.2和 actix-multipart v0.3.0 . 举一个最小的例子, use actix_multi
在actix-web documentation is only an example如何接收唯一命名的查询参数。 但是我如何接收同名的多个查询参数呢?例如: http://localhost:808
我正在使用 sqlx 访问数据库的 actix-web 2.0.0 中实现身份验证提取器。我有这个代码: use actix_web::{dev, web, Error, HttpRequest, F
我有一个正在实现的 API,其中有一个需要调用的昂贵函数。我想记住这个函数或使用键值缓存来查找以前的结果。我也会考虑 Mutex或类似的结构,但我想避免诸如 Redis 或 Memcached 之类的
我是一名优秀的程序员,十分优秀!