pub struct AppError {
pub code: String,
pub message: String,
pub message_detail: Option<String>,
pub context: Value,
pub source: Option<Error>,
}Expand description
应用程序统一错误类型
提供结构化的错误处理,支持错误链、上下文信息和序列化。 设计用于替代简单的字符串错误,提供更丰富的错误信息。
§特性
- 错误代码: 机器可读的错误标识符
- 错误消息: 人类可读的错误描述
- 详细信息: 可选的详细错误信息
- 上下文: JSON 格式的上下文数据
- 错误链: 支持原始异常和堆栈追踪
§示例
use knife_util::AppError;
let error = AppError::new("FILE_NOT_FOUND", "配置文件不存在")
.with_message_detail("路径: /etc/app/config.toml")
.with_attribute("file_path", "/etc/app/config.toml");Fields§
§code: String错误代码 - 机器可读的错误标识符
message: String错误消息 - 人类可读的错误描述
message_detail: Option<String>详细错误信息 - 可选的额外错误详情
context: Value上下文信息 - JSON 格式的上下文数据
source: Option<Error>原始异常和堆栈追踪 - 不参与序列化
Implementations§
Source§impl AppError
impl AppError
Sourcepub fn message_detail(&self) -> Option<&str>
pub fn message_detail(&self) -> Option<&str>
获取详细错误信息
Sourcepub fn with_message_detail(self, detail: &str) -> Self
pub fn with_message_detail(self, detail: &str) -> Self
设置详细错误信息
Sourcepub fn with_attribute(self, key: &str, value: impl Into<Value>) -> Self
pub fn with_attribute(self, key: &str, value: impl Into<Value>) -> Self
添加上下文信息
Sourcepub fn get_attribute(&self, key: &str) -> Option<&Value>
pub fn get_attribute(&self, key: &str) -> Option<&Value>
获取上下文信息
Sourcepub fn with_context(self, context: Value) -> Self
pub fn with_context(self, context: Value) -> Self
设置完整的上下文信息
Sourcepub fn source(&self) -> Option<&AnyhowError>
pub fn source(&self) -> Option<&AnyhowError>
获取原始异常信息
Sourcepub fn with_source(self, source: AnyhowError) -> Self
pub fn with_source(self, source: AnyhowError) -> Self
设置原始异常信息
Sourcepub fn from_error<E: Error + Send + Sync + 'static>(
code: &str,
message: &str,
err: E,
) -> Self
pub fn from_error<E: Error + Send + Sync + 'static>( code: &str, message: &str, err: E, ) -> Self
从其他错误类型创建AppError
Sourcepub fn from_anyhow(code: &str, message: &str, err: AnyhowError) -> Self
pub fn from_anyhow(code: &str, message: &str, err: AnyhowError) -> Self
从anyhow::Error创建AppError
Sourcepub fn from_display<E: Display>(code: &str, message: &str, err: E) -> Self
pub fn from_display<E: Display>(code: &str, message: &str, err: E) -> Self
从Display类型创建AppError
Trait Implementations§
Source§impl<'de> Deserialize<'de> for AppError
impl<'de> Deserialize<'de> for AppError
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Error for AppError
impl Error for AppError
1.30.0 · 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()
Source§impl From<TryInitError> for AppError
impl From<TryInitError> for AppError
Source§fn from(err: TryInitError) -> Self
fn from(err: TryInitError) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for AppError
impl RefUnwindSafe for AppError
impl Send for AppError
impl Sync for AppError
impl Unpin 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