Skip to main content

Error

Struct Error 

Source
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

Source

pub fn new(kind: Kind, msg: impl Into<String>) -> Self

新建一条没有 cause 的分类错误。

Source

pub fn errorf(kind: Kind, format: Arguments<'_>) -> Self

用格式串新建一条分类错误。

Source

pub fn wrap(kind: Kind, cause: impl Error + Send + Sync + 'static) -> Self

把任意错误包成分类错误:消息取 cause 的消息。

Source

pub fn wrap_msg( kind: Kind, cause: impl Error + Send + Sync + 'static, msg: impl Into<String>, ) -> Self

既有消息又有 cause 的分类错误。

Source

pub fn upgrade(cause: impl Error + Send + Sync + 'static) -> Self

把任意用户错误升级为库错误:只包一层(返回 Box 进 source 链), classify 会沿链找用户内层已有的分类。用户错误本身不丢。

Source

pub fn with_code(self, code: impl Into<String>) -> Self

§8.5 code:自由域标识,链式挂载(不改变 Kind 映射)。

Source

pub fn with_detail(self, key: impl Into<String>, value: Value) -> Self

§8.5 detail:单条结构化上下文,链式挂载。

Source

pub fn with_details(self, details: Map<String, Value>) -> Self

§8.5 detail:批量合并结构化上下文,链式挂载。

Source

pub fn with_status(self, status: u16) -> Self

§8.5 status:覆盖 Kind 推导的 HTTP 状态(仅 HTTP 通道生效)。

Source

pub fn inherit_rich(self, src: &(dyn Error + 'static)) -> Self

从错误链继承富化层(code/detail/status 各取首个非空)——管线包装 壳用:handler 已是富化 errors::Error 时,包装不丢 §8.5 语义。

Source

pub fn kind(&self) -> Kind

Source

pub fn status_override(&self) -> Option<u16>

§8.5 HTTP 状态覆盖(None = 按 Kind 推导)。

Source

pub fn cause(&self) -> Option<&(dyn Error + Send + Sync)>

解开一层到最内层已知 cause(Go 的 Cause)。

Trait Implementations§

Source§

impl Debug for Error

Source§

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

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

impl Display for Error

Source§

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

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

impl Error for Error

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<Error> for Error

Source§

fn from(e: Error) -> Self

Converts to this type from the input type.

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> 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<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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
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