8 个版本
0.2.7 | 2024 年 6 月 16 日 |
---|---|
0.2.6 | 2024 年 6 月 14 日 |
0.2.1 | 2024 年 3 月 10 日 |
0.2.0 | 2023 年 6 月 13 日 |
0.1.7 | 2023 年 4 月 26 日 |
#115 in HTTP 服务器
每月 12,873 次下载
12KB
116 行
actix-web-thiserror
一个 crate,它扩展了 thiserror crate 的功能,以自动返回适当的 actix-web 响应。
文档
错误定义
use actix_web_thiserror::ResponseError;
use thiserror::Error;
#[derive(Debug, Error, ResponseError)]
pub enum Base64ImageError {
#[response(reason = "INVALID_IMAGE_FORMAT")]
#[error("invalid image format")]
InvalidImageFormat,
#[response(reason = "INVALID_STRING")]
#[error("invalid string")]
InvalidString,
}
错误实现
pub async fn error_test() -> Result<HttpResponse, Error> {
Err(Base64ImageError::InvalidImageFormat)?
}
错误响应
reason
是一个字符串,可以根据适当的情况以某种形式提供给客户端来解释错误。在这里,它是一个可以本地化的枚举。
注意:此响应已由 ResponseTransform
格式化。要指定自定义的 ResponseTransform,请实现 ResponseTransform
并在您的 derive 下添加 #[response)]
。
{
"result": 0,
"reason": "INVALID_IMAGE_FORMAT"
}
错误记录
当错误通过 http 响应返回时,错误文本会自动打印到日志中。
Apr 23 02:19:35.211 ERROR Response error: invalid image format
Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler
依赖项
~15–26MB
~465K SLoC