rust - 处理拒绝时获取当前路径
问题描述
我想知道如何在 Warp 的拒绝处理程序中获取 HTTP 路径?我有以下拒绝方法:
pub(crate) async fn handle(err: Rejection) -> Result<impl Reply, Infallible> {
let response = if err.is_not_found() {
HttpApiProblem::with_title_and_type_from_status(StatusCode::NOT_FOUND)
} else if let Some(e) = err.find::<warp::filters::body::BodyDeserializeError>() {
HttpApiProblem::with_title_and_type_from_status(StatusCode::BAD_REQUEST)
.set_detail(format!("{}", e))
} else if let Some(e) = err.find::<Error>() {
handle_request_error(e)
} else if let Some(e) = err.find::<warp::reject::MethodNotAllowed>() {
HttpApiProblem::with_title_and_type_from_status(StatusCode::METHOD_NOT_ALLOWED)
.set_detail(format!("{}", e))
} else {
error!("handle_rejection catch all: {:?}", err);
HttpApiProblem::with_title_and_type_from_status(StatusCode::INTERNAL_SERVER_ERROR)
};
Ok(response.to_hyper_response())
}
例如,我会打电话curl localhost:1234/this-aint-valid-path/123
并希望能够访问以/this-aint-valid-path/123
进行日志记录,并将其作为错误响应的一部分返回。
解决方案
它看起来像方法err.is_not_found()
检查原因是否匹配Reason::NotFound
,这是一个没有参数的枚举变体。Rejection
结构没有超出其原因的其他元数据,因此无法修改您问题中的代码来解决您的问题。但是,可以使用您想要的任何元数据创建自定义原因。您正在寻找的用于创建该Rejection
对象的方法称为custom
,并且可以在此处找到它的文档。
推荐阅读
- html - 如何在固定 div 之后定位非固定 div?
- python - 得到一个像 TypeError: cannot unpack non-iterable float object 这样的错误
- python - GreedyAgent() 类的实例如何使用变量而不创建任何变量?
- angular - 如何从中获取值
在角度8? - mongodb - 仅缓存 MongoDB 中的高使用率键
- flutter - 如何在 web 应用程序的颤振运行中启用热重载?
- python-3.x - 如何遍历Python列表中的所有项目?
- node.js - 使用 mongoose express js 更新用户个人资料页面
- angular - Node 中的错误不存在:C:/projects/ma/CustomerPortal-master/node_modules/ngx-smart-table
- c++ - 无法在 QT 上连接数据库