Skip to main content

ErrorPages

Struct ErrorPages 

Source
pub struct ErrorPages { /* private fields */ }
Expand description

错误页定制器。

作为中间件挂在尽可能外层(CatchPanic 之内即可):它要能改到由内层 中间件与 handler 产生的错误响应。

Implementations§

Source§

impl ErrorPages

Source

pub fn new() -> ErrorPages

Source

pub fn on(self, status: StatusCode, target: &str) -> Result<ErrorPages>

error_page <code> <path>;

Source

pub fn on_with_status( self, status: StatusCode, new_status: StatusCode, target: &str, ) -> Result<ErrorPages>

error_page <code> =<new> <path>;(把状态码也换掉;需逐条显式声明)

Source

pub fn on_many( self, statuses: &[StatusCode], target: &str, ) -> Result<ErrorPages>

一条规则覆盖多个状态码(Nginx 的多码写法)。

Source

pub fn fallback(self, target: &str) -> Result<ErrorPages>

未列出状态码的统一回落页。

Source

pub fn recursive(self, on: bool) -> ErrorPages

recursive_error_pages on|off;(默认 off)

Source

pub fn len(&self) -> usize

规则条数(观测用)。

Source

pub fn is_empty(&self) -> bool

Source

pub fn rule_for(&self, status: StatusCode) -> Option<&Rule>

取某状态码对应的规则。

Source

pub fn rewrite<F>(&self, resp: Response, fetch: F) -> Response
where F: FnOnce(&str) -> Option<Response>,

把一条已被内层判定为错误的响应换成错误页。

这不是中间件:中间件版本见 [ErrorPages::layer],而这里是纯函数形式, 供连接层/静态引擎这类“不经过洋葱链“的出错路径复用同一个映射 (协议错误的 400/413/431 由 xorevia-rt 直接产出,压根不进中间件链)。

fetch 负责把内部路径服务成实际页面(通常是静态引擎或某个 handler); 返回 None 表示取不到页面,此时保留原错误响应——绝不因为 “错误页自身失败“就把一个 404 变成 200 或空响应。

Source

pub fn wrap<S>(self, inner: Arc<dyn Endpoint<S>>) -> Arc<dyn Endpoint<S>> ⓘ
where S: Clone + Send + Sync + 'static,

端点形态:包住应用的真实派发器,把错误状态的目标路径当成一次内部 GET 重新派发(App::error_pages 用的就是这个入口)。

为什么没有“纯中间件“版本:中间件的 Next 走到 endpoint 就消费完请求, 无法从头再派发;塞一个永远返回 None 的 fetch 只会得到假开关。

Trait Implementations§

Source§

impl Clone for ErrorPages

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ErrorPages

Source§

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

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

impl Default for ErrorPages

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for ErrorPages

§

impl Freeze for ErrorPages

§

impl Move for ErrorPages

§

impl RefUnwindSafe for ErrorPages

§

impl Send for ErrorPages
where RawTable<(u16, Rule)>: Send, RawVecInner: Send,

§

impl Sync for ErrorPages
where RawTable<(u16, Rule)>: Sync, RawVecInner: Sync,

§

impl Unpin for ErrorPages
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for ErrorPages

§

impl UnwindSafe for ErrorPages
where HashMap<u16, Rule>: UnwindSafe, RawVecInner: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.