pub struct ApiError {
pub code: i32,
pub msg: String,
pub status: u16,
pub internal_detail: Option<String>,
}Expand description
API 错误(对外暴露的统一错误类型)
HTTP 状态码使用 u16 存储,与 Web 框架解耦。
Fields§
§code: i32业务码
msg: String对外消息(已脱敏,不泄露内部信息)
status: u16HTTP 状态码(u16,与 axum::StatusCode 互转)
internal_detail: Option<String>内部调试信息(仅日志记录,不返回前端)
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn new(status: u16, code: i32, msg: impl Into<String>) -> Self
pub fn new(status: u16, code: i32, msg: impl Into<String>) -> Self
创建 API 错误
status: HTTP 状态码(如 400、401、500)code: 业务错误码msg: 对外提示消息
Sourcepub fn bad_request(msg: impl Into<String>) -> Self
pub fn bad_request(msg: impl Into<String>) -> Self
400 Bad Request:客户端请求格式或参数错误
401 Unauthorized:未认证或 Token 无效
Sourcepub fn method_not_allowed(msg: impl Into<String>) -> Self
pub fn method_not_allowed(msg: impl Into<String>) -> Self
405 Method Not Allowed:HTTP 方法不正确
Sourcepub fn unprocessable_entity(msg: impl Into<String>) -> Self
pub fn unprocessable_entity(msg: impl Into<String>) -> Self
422 Unprocessable Entity:请求体语义错误(如字段校验失败)
Sourcepub fn too_many_requests(msg: impl Into<String>) -> Self
pub fn too_many_requests(msg: impl Into<String>) -> Self
429 Too Many Requests:请求频率超限
Sourcepub fn internal(msg: impl Into<String>) -> Self
pub fn internal(msg: impl Into<String>) -> Self
500 Internal Server Error:服务端内部错误
前端仅看到模糊提示;完整错误需通过日志排查。
Sourcepub fn internal_masked(
public_msg: impl Into<String>,
detail: impl Into<String>,
) -> Self
pub fn internal_masked( public_msg: impl Into<String>, detail: impl Into<String>, ) -> Self
500 Internal Server Error(带调试详情)
public_msg: 返回前端的模糊提示detail: 内部日志记录的详细信息(如完整的错误栈)
503 Service Unavailable:服务暂时不可用
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ApiError
impl RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl UnsafeUnpin for ApiError
impl UnwindSafe for ApiError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more