Skip to main content

ApiError

Struct ApiError 

Source
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: u16

HTTP 状态码(u16,与 axum::StatusCode 互转)

§internal_detail: Option<String>

内部调试信息(仅日志记录,不返回前端)

Implementations§

Source§

impl ApiError

Source

pub fn new(status: u16, code: i32, msg: impl Into<String>) -> Self

创建 API 错误

  • status: HTTP 状态码(如 400、401、500)
  • code: 业务错误码
  • msg: 对外提示消息
Source

pub fn bad_request(msg: impl Into<String>) -> Self

400 Bad Request:客户端请求格式或参数错误

Source

pub fn unauthorized(msg: impl Into<String>) -> Self

401 Unauthorized:未认证或 Token 无效

Source

pub fn forbidden(msg: impl Into<String>) -> Self

403 Forbidden:已认证但权限不足

Source

pub fn not_found(msg: impl Into<String>) -> Self

404 Not Found:资源不存在

Source

pub fn method_not_allowed(msg: impl Into<String>) -> Self

405 Method Not Allowed:HTTP 方法不正确

Source

pub fn conflict(msg: impl Into<String>) -> Self

409 Conflict:资源冲突(如重复创建)

Source

pub fn unprocessable_entity(msg: impl Into<String>) -> Self

422 Unprocessable Entity:请求体语义错误(如字段校验失败)

Source

pub fn too_many_requests(msg: impl Into<String>) -> Self

429 Too Many Requests:请求频率超限

Source

pub fn internal(msg: impl Into<String>) -> Self

500 Internal Server Error:服务端内部错误

前端仅看到模糊提示;完整错误需通过日志排查。

Source

pub fn internal_masked( public_msg: impl Into<String>, detail: impl Into<String>, ) -> Self

500 Internal Server Error(带调试详情)

  • public_msg: 返回前端的模糊提示
  • detail: 内部日志记录的详细信息(如完整的错误栈)
Source

pub fn service_unavailable(msg: impl Into<String>) -> Self

503 Service Unavailable:服务暂时不可用

Trait Implementations§

Source§

impl Debug for ApiError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<ApiError> for Error

Source§

fn from(e: ApiError) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for ApiError

Source§

fn from(e: Error) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more