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
业务错误码(归一化值类型,零堆分配)
WithContext
带上下文的业务错误(错误码 + 动态信息)
Internal(Error)
内部错误(IO / JSON / 配置 / 第三方库 / anyhow)
插件直接用 anyhow::Result + ? 即可自动走这条路径。
Implementations§
Source§impl AppError
impl AppError
pub fn domain(&self) -> &'static str
pub fn code(&self) -> i32
Sourcepub fn message(&self) -> &str
pub fn message(&self) -> &str
获取错误消息。
- 对
ErrCode/WithContext返回静态消息字符串 - 对
Internal返回"Internal error"(不泄漏内存)
如需包含 Internal 错误的完整信息,请使用 full_message()。
Sourcepub fn full_message(&self) -> String
pub fn full_message(&self) -> String
完整消息(静态消息 + 上下文,或内部错误链)
Sourcepub fn err_code(&self) -> AppErrCode
pub fn err_code(&self) -> AppErrCode
获取 AppErrCode(丢弃上下文和内部错误细节)
Sourcepub fn is_same_kind(&self, other: &AppError) -> bool
pub fn is_same_kind(&self, other: &AppError) -> bool
是否为同一类错误
Sourcepub fn is_internal(&self) -> bool
pub fn is_internal(&self) -> bool
是否为内部错误
Trait Implementations§
impl Eq for AppError
Source§impl Error for AppError
impl Error for AppError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for AppError
impl RefUnwindSafe for AppError
impl Send for AppError
impl Sync for AppError
impl Unpin for AppError
impl UnsafeUnpin for AppError
impl UnwindSafe for AppError
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