pub struct Error { /* private fields */ }Expand description
Error 是库内统一的错误类型(CodedError 的 Rust 对应物)。
kind 缺省为 Internal;用户在自己的 handler 里也可以直接返回任何
std::error::Error(由 classify 兜底分类为 Internal)。
spec §8.5:可选富化层由 builder 链挂载——
Error::new(NotFound, "user 7").with_code("USER_NOT_FOUND") .with_detail("user_id", json!(7)).with_status(410)。
Implementations§
Source§impl Error
impl Error
Sourcepub fn wrap(kind: Kind, cause: impl Error + Send + Sync + 'static) -> Self
pub fn wrap(kind: Kind, cause: impl Error + Send + Sync + 'static) -> Self
把任意错误包成分类错误:消息取 cause 的消息。
Sourcepub fn wrap_msg(
kind: Kind,
cause: impl Error + Send + Sync + 'static,
msg: impl Into<String>,
) -> Self
pub fn wrap_msg( kind: Kind, cause: impl Error + Send + Sync + 'static, msg: impl Into<String>, ) -> Self
既有消息又有 cause 的分类错误。
Sourcepub fn upgrade(cause: impl Error + Send + Sync + 'static) -> Self
pub fn upgrade(cause: impl Error + Send + Sync + 'static) -> Self
把任意用户错误升级为库错误:只包一层(返回 Box 进 source 链), classify 会沿链找用户内层已有的分类。用户错误本身不丢。
Sourcepub fn with_detail(self, key: impl Into<String>, value: Value) -> Self
pub fn with_detail(self, key: impl Into<String>, value: Value) -> Self
§8.5 detail:单条结构化上下文,链式挂载。
Sourcepub fn with_details(self, details: Map<String, Value>) -> Self
pub fn with_details(self, details: Map<String, Value>) -> Self
§8.5 detail:批量合并结构化上下文,链式挂载。
Sourcepub fn with_status(self, status: u16) -> Self
pub fn with_status(self, status: u16) -> Self
§8.5 status:覆盖 Kind 推导的 HTTP 状态(仅 HTTP 通道生效)。
Sourcepub fn inherit_rich(self, src: &(dyn Error + 'static)) -> Self
pub fn inherit_rich(self, src: &(dyn Error + 'static)) -> Self
从错误链继承富化层(code/detail/status 各取首个非空)——管线包装 壳用:handler 已是富化 errors::Error 时,包装不丢 §8.5 语义。
pub fn kind(&self) -> Kind
Sourcepub fn status_override(&self) -> Option<u16>
pub fn status_override(&self) -> Option<u16>
§8.5 HTTP 状态覆盖(None = 按 Kind 推导)。
Trait Implementations§
Source§impl Error for Error
impl Error for Error
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 !RefUnwindSafe for Error
impl !UnwindSafe for Error
impl Freeze for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more