From 520eebfe13c108f7aa5bf806d170e4ba497a9fda Mon Sep 17 00:00:00 2001 From: arctic-hen7 Date: Tue, 10 Jan 2023 09:38:05 +1100 Subject: [PATCH] feat: added simple error views example to basic example --- examples/core/basic/src/error_views.rs | 62 ++++++++++++++++++++++ examples/core/basic/src/main.rs | 4 +- examples/core/basic/src/templates/index.rs | 18 +++---- 3 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 examples/core/basic/src/error_views.rs diff --git a/examples/core/basic/src/error_views.rs b/examples/core/basic/src/error_views.rs new file mode 100644 index 0000000000..bf523a1833 --- /dev/null +++ b/examples/core/basic/src/error_views.rs @@ -0,0 +1,62 @@ +use perseus::errors::ClientError; +use perseus::prelude::*; +use sycamore::prelude::*; + +pub fn get_error_views() -> ErrorViews { + ErrorViews::new(|cx, err, _err_info, _err_pos| { + match err { + ClientError::ServerError { status, message: _ } => match status { + 404 => ( + view! { cx, + title { "Page not found" } + }, + view! { cx, + p { "Sorry, that page doesn't seem to exist." } + }, + ), + // 4xx is a client error + _ if (400..500).contains(&status) => ( + view! { cx, + title { "Error" } + }, + view! { cx, + p { "There was something wrong with the last request, please try reloading the page." } + }, + ), + // 5xx is a server error + _ => ( + view! { cx, + title { "Error" } + }, + view! { cx, + p { "Sorry, our server experienced an internal error. Please try reloading the page." } + }, + ), + }, + ClientError::Panic(_) => ( + view! { cx, + title { "Critical error" } + }, + view! { cx, + p { "Sorry, but a critical internal error has occurred. This has been automatically reported to our team, who'll get on it as soon as possible. In the mean time, please try reloading the page." } + }, + ), + ClientError::FetchError(_) => ( + view! { cx, + title { "Error" } + }, + view! { cx, + p { "A network error occurred, do you have an internet connection? (If you do, try reloading the page.)" } + }, + ), + _ => ( + view! { cx, + title { "Error" } + }, + view! { cx, + p { (format!("An internal error has occurred: '{}'.", err)) } + }, + ), + } + }) +} diff --git a/examples/core/basic/src/main.rs b/examples/core/basic/src/main.rs index 90aa76de38..c05dabdd04 100644 --- a/examples/core/basic/src/main.rs +++ b/examples/core/basic/src/main.rs @@ -1,3 +1,4 @@ +mod error_views; mod templates; use perseus::prelude::*; @@ -7,6 +8,5 @@ pub fn main() -> PerseusApp { PerseusApp::new() .template(crate::templates::index::get_template()) .template(crate::templates::about::get_template()) - // This is for example usage only, you should specify your own error pages - .error_views(ErrorViews::unlocalized_development_default()) + .error_views(crate::error_views::get_error_views()) } diff --git a/examples/core/basic/src/templates/index.rs b/examples/core/basic/src/templates/index.rs index 160a1a9e92..09ef5e05f7 100644 --- a/examples/core/basic/src/templates/index.rs +++ b/examples/core/basic/src/templates/index.rs @@ -5,7 +5,7 @@ use sycamore::prelude::*; #[derive(Serialize, Deserialize, ReactiveState, Clone)] #[rx(alias = "IndexPageStateRx")] struct IndexPageState { - pub greeting: String, + greeting: String, } #[auto_scope] @@ -16,14 +16,6 @@ fn index_page(cx: Scope, state: &IndexPageStateRx) -> View { } } -pub fn get_template() -> Template { - Template::build("index") - .build_state_fn(get_build_state) - .view_with_state(index_page) - .head_with_state(head) - .build() -} - #[engine_only_fn] fn head(cx: Scope, _props: IndexPageState) -> View { view! { cx, @@ -37,3 +29,11 @@ async fn get_build_state(_info: StateGeneratorInfo<()>) -> IndexPageState { greeting: "Hello World!".to_string(), } } + +pub fn get_template() -> Template { + Template::build("index") + .build_state_fn(get_build_state) + .view_with_state(index_page) + .head_with_state(head) + .build() +}