pub struct Error {
pub status: u16,
pub category: String,
pub message: String,
/* private fields */
}Expand description
全局 HTTP 错误类型,贯穿整个应用。
category 与 message 始终存在,直接置于结构体上。
可选字段通过 Box<ErrorData> 装箱,将 Err 变体保持在
result_large_err 的 128 字节限制以内。
Fields§
§status: u16HTTP 状态码,0 表示未设置,IntoResponse 时回退为 500。
category: String错误来源模块或分类,如 “cache”、“db”。
message: String面向用户或日志的错误描述信息。
Implementations§
Source§impl Error
impl Error
Sourcepub fn with_category(self, category: impl ToString) -> Self
pub fn with_category(self, category: impl ToString) -> Self
设置错误分类(模块来源),支持链式调用。
Sourcepub fn with_sub_category(self, sub_category: impl ToString) -> Self
pub fn with_sub_category(self, sub_category: impl ToString) -> 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
标记是否为需要告警的异常级错误,支持链式调用。
Trait Implementations§
Source§impl Deref for Error
通过 Deref/DerefMut 将 ErrorData 的可选字段(sub_category、
code、exception、extra)直接暴露在 Error 上,无需手动访问 .data。
impl Deref for Error
通过 Deref/DerefMut 将 ErrorData 的可选字段(sub_category、
code、exception、extra)直接暴露在 Error 上,无需手动访问 .data。
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,
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