pub struct Error { /* private fields */ }Expand description
全局 HTTP 错误类型,贯穿整个应用。
所有字段均为私有,必须通过 Error::new 创建并经由链式 with_xxx /
add_xxx 方法配置;读取使用同名 getter(Error::status、
Error::category 等)。
内部把可选字段统一装箱到 Box<ErrorData>,将 Result<_, Error> 的
Err 变体保持在 clippy::result_large_err 128 字节限制以内。
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(message: impl ToString) -> Self
pub fn new(message: impl ToString) -> Self
以错误信息创建新的 Error 实例,其余字段均为默认值。
message 接受任意 Display 类型,便于直接由外部错误包装。
Sourcepub fn with_category(self, category: impl Into<String>) -> Self
pub fn with_category(self, category: impl Into<String>) -> Self
设置错误分类(模块来源),支持链式调用。
Sourcepub fn with_sub_category(self, sub_category: impl Into<String>) -> Self
pub fn with_sub_category(self, sub_category: impl Into<String>) -> Self
设置错误子分类,支持链式调用。
Sourcepub fn with_status(self, status: u16) -> Self
pub fn with_status(self, status: u16) -> Self
设置 HTTP 状态码,支持链式调用。
Sourcepub fn with_exception(self, exception: bool) -> Self
pub fn with_exception(self, exception: bool) -> Self
标记是否为需要告警的异常级错误,支持链式调用。
Sourcepub fn sub_category(&self) -> Option<&str>
pub fn sub_category(&self) -> Option<&str>
错误子分类,未设置时返回 None。
Sourcepub fn is_exception(&self) -> bool
pub fn is_exception(&self) -> bool
是否为需要告警的异常级错误;未显式设置时返回 false。
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Error
impl<'de> Deserialize<'de> for Error
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 Error
impl Error for Error
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 IntoResponse for Error
将 Error 转换为带 JSON 响应体和 no-cache 头的 HTTP 响应。
impl IntoResponse for Error
将 Error 转换为带 JSON 响应体和 no-cache 头的 HTTP 响应。
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Auto Trait Implementations§
impl Freeze for Error
impl RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl UnsafeUnpin for Error
impl UnwindSafe 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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Call the handler with the given request.
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
Apply a
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Convert the handler into a
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Convert the handler into a
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
Convert the handler into a
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
Convert the handler into a
MakeService which stores information
about the incoming connection and has no state. Read more