Struct perseus::error_pages::ErrorPages
source · [−]pub struct ErrorPages<G: Html> { /* private fields */ }
Expand description
A representation of the views configured in an app for responding to errors.
On the web, errors occur frequently beyond app logic, usually in communication with servers, which will return HTTP status codes that indicate a success or failure. If a non-success error code is received, then Perseus will automatically render the appropriate error page, based on that status code. If no page has been explicitly constructed for that status code, then the fallback page will be used.
Each error page is a closure returning a [View
] that takes four
parameters: a reactive scope, the URL the user was on when the error
occurred (which they’ll still be on, no route change occurs when rendering
an error page), the status code itself, a String
of the actual error
message, and a Translator
(which may not be available if the error
occurred before translations data could be fetched and processed, in which
case you should try to display language-agnostic information).
In development, you can get away with not defining any error pages for your
app, as Perseus has a simple inbuilt default, though, when you try to go to
production (e.g. with perseus deploy
), you’ll receive an error message in
building. In other words, you must define your own error pages for release
mode.
Implementations
sourceimpl<G: Html> ErrorPages<G>
impl<G: Html> ErrorPages<G>
sourcepub fn new(
fallback: impl Fn(Scope<'_>, String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync + 'static
) -> Self
pub fn new(
fallback: impl Fn(Scope<'_>, String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync + 'static
) -> Self
Creates a new definition of error pages with just a fallback page, which will be used when an error occurs whose status code has not been explicitly handled by some other error page.
sourcepub fn add_page(
&mut self,
status: u16,
page: impl Fn(Scope<'_>, String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync + 'static
)
pub fn add_page(
&mut self,
status: u16,
page: impl Fn(Scope<'_>, String, u16, String, Option<Rc<Translator>>) -> View<G> + Send + Sync + 'static
)
Adds a new page for the given status code. If a page was already defined
for the given code, it will be updated by replacement, through the
mechanics of the internal HashMap
. While there is no requirement
for this to be a valid HTTP status code, there would be no point in
defining a handler for a status code not on this list
sourcepub fn add_page_rc(&mut self, status: u16, page: ErrorPageTemplate<G>)
pub fn add_page_rc(&mut self, status: u16, page: ErrorPageTemplate<G>)
Adds a new page for the given status code. If a page was already defined
for the given code, it will be updated by the mechanics of
the internal HashMap
. This differs from .add_page()
in that it takes
an Rc
, which can be useful for plugins.
sourceimpl ErrorPages<SsrNode>
impl ErrorPages<SsrNode>
sourcepub fn render_to_string(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>
) -> String
pub fn render_to_string(
&self,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>
) -> String
Renders the error page to a string. This should then be hydrated on the client-side. No reactive scope is provided to this function, it uses an internal one.
sourcepub fn render_to_string_scoped(
&self,
cx: Scope<'_>,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>
) -> String
pub fn render_to_string_scoped(
&self,
cx: Scope<'_>,
url: &str,
status: u16,
err: &str,
translator: Option<Rc<Translator>>
) -> String
Renders the error page to a string, using the given reactive scope. Note
that this function is not used internally, and .render_to_string()
should cover all uses. This is included for completeness.
Trait Implementations
sourceimpl<G: Html> Debug for ErrorPages<G>
impl<G: Html> Debug for ErrorPages<G>
Auto Trait Implementations
impl<G> !RefUnwindSafe for ErrorPages<G>
impl<G> Send for ErrorPages<G>
impl<G> Sync for ErrorPages<G>
impl<G> Unpin for ErrorPages<G>
impl<G> !UnwindSafe for ErrorPages<G>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more