pub struct AppError {
pub code: String,
pub message: String,
pub message_detail: Option<String>,
pub context: Value,
pub source: Option<Error>,
}Expand description
应用程序统一错误类型
Fields§
§code: String错误代码
message: String错误消息
message_detail: Option<String>详细错误信息
context: Value上下文信息
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 source(&self) -> Option<&AnyhowError>
pub fn source(&self) -> Option<&AnyhowError>
获取原始异常信息
Sourcepub fn with_context(self, context: Value) -> Self
pub fn with_context(self, context: Value) -> Self
设置完整的上下文信息
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<AppError> for Error
从AppError转换为std::io::Error
impl From<AppError> for Error
从AppError转换为std::io::Error
支持在返回 std::io::Result 的函数中使用 ? 操作符
§示例
use knife_util::AppError;
use std::fs::File;
fn read_file() -> std::io::Result<String> {
let mut file = File::open("nonexistent.txt")?; // 自动转换为 AppError
// ... 其他操作
Ok("content".to_string())
}Source§impl From<Error> for AppError
从std::io::Error转换为AppError
impl From<Error> for AppError
从std::io::Error转换为AppError
支持通过 ? 操作符自动处理 IOError
§示例
use knife_util::AppError;
use std::fs::File;
fn read_file() -> Result<String, AppError> {
let mut file = File::open("nonexistent.txt")?; // 自动转换为 AppError
// ... 其他操作
Ok("content".to_string())
}Source§impl From<TryInitError> for AppError
从TryInitError转换为AppError
impl From<TryInitError> for AppError
从TryInitError转换为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