Skip to main content

AppError

Enum AppError 

Source
pub enum AppError {
    ErrCode {
        domain: &'static str,
        code: i32,
        message: &'static str,
    },
    WithContext {
        domain: &'static str,
        code: i32,
        message: &'static str,
        context: String,
    },
    Internal(Error),
}
Expand description

统一错误类型。

所有错误统一走这一种类型,Result<T, AppError> 贯穿全栈。 不实现 Clone(因为 anyhow::Error 不是 Clone)。

Variants§

§

ErrCode

业务错误码(归一化值类型,零堆分配)

Fields

§domain: &'static str
§code: i32
§message: &'static str
§

WithContext

带上下文的业务错误(错误码 + 动态信息)

Fields

§domain: &'static str
§code: i32
§message: &'static str
§context: String
§

Internal(Error)

内部错误(IO / JSON / 配置 / 第三方库 / anyhow) 插件直接用 anyhow::Result + ? 即可自动走这条路径。

Implementations§

Source§

impl AppError

Source

pub fn from_code<C>(code: C) -> AppError
where C: CodeMsg,

从业务错误码构造

Source

pub fn with_context<C>(code: C, context: impl Into<String>) -> AppError
where C: CodeMsg,

带上下文构造

Source

pub fn from_anyhow(err: Error) -> AppError

从 anyhow 构造内部错误

Source§

impl AppError

Source

pub fn domain(&self) -> &'static str

Source

pub fn code(&self) -> i32

Source

pub fn message(&self) -> &str

获取错误消息。

  • ErrCode / WithContext 返回静态消息字符串
  • Internal 返回 "Internal error"(不泄漏内存)

如需包含 Internal 错误的完整信息,请使用 full_message()

Source

pub fn context(&self) -> Option<&str>

获取上下文(如果有)

Source

pub fn internal(&self) -> Option<&Error>

获取内部错误(如果是 Internal)

Source

pub fn full_message(&self) -> String

完整消息(静态消息 + 上下文,或内部错误链)

Source

pub fn err_code(&self) -> AppErrCode

获取 AppErrCode(丢弃上下文和内部错误细节)

Source

pub fn is_same_kind(&self, other: &AppError) -> bool

是否为同一类错误

Source

pub fn is_internal(&self) -> bool

是否为内部错误

Trait Implementations§

Source§

impl Debug for AppError

Source§

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

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

impl Display for AppError

Source§

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

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

impl Eq for AppError

Source§

impl Error for AppError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<&str> for AppError

Source§

fn from(s: &str) -> AppError

Converts to this type from the input type.
Source§

impl From<AppError> for ApiR<()>

AppError → ApiRes 转换

Source§

fn from(err: AppError) -> ApiR<()>

Converts to this type from the input type.
Source§

impl From<DiErr> for AppError

Source§

fn from(e: DiErr) -> AppError

Converts to this type from the input type.
Source§

impl From<DiErr> for AppError

Source§

fn from(e: DiErr) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for AppError

Source§

fn from(err: Error) -> AppError

Converts to this type from the input type.
Source§

impl From<Error> for AppError

Source§

fn from(err: Error) -> AppError

Converts to this type from the input type.
Source§

impl From<Error> for AppError

Source§

fn from(err: Error) -> AppError

Converts to this type from the input type.
Source§

impl From<Error> for AppError

Source§

fn from(err: Error) -> AppError

Converts to this type from the input type.
Source§

impl From<String> for AppError

Source§

fn from(s: String) -> AppError

Converts to this type from the input type.
Source§

impl PartialEq for AppError

Source§

fn eq(&self, other: &AppError) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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