Skip to content

Commit

Permalink
feat: added simple error views example to basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 9, 2023
1 parent 746cdc3 commit 520eebf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
62 changes: 62 additions & 0 deletions examples/core/basic/src/error_views.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use perseus::errors::ClientError;
use perseus::prelude::*;
use sycamore::prelude::*;

pub fn get_error_views<G: Html>() -> ErrorViews<G> {
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)) }
},
),
}
})
}
4 changes: 2 additions & 2 deletions examples/core/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod error_views;
mod templates;

use perseus::prelude::*;
Expand All @@ -7,6 +8,5 @@ pub fn main<G: Html>() -> PerseusApp<G> {
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())
}
18 changes: 9 additions & 9 deletions examples/core/basic/src/templates/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sycamore::prelude::*;
#[derive(Serialize, Deserialize, ReactiveState, Clone)]
#[rx(alias = "IndexPageStateRx")]
struct IndexPageState {
pub greeting: String,
greeting: String,
}

#[auto_scope]
Expand All @@ -16,14 +16,6 @@ fn index_page<G: Html>(cx: Scope, state: &IndexPageStateRx) -> View<G> {
}
}

pub fn get_template<G: Html>() -> Template<G> {
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<SsrNode> {
view! { cx,
Expand All @@ -37,3 +29,11 @@ async fn get_build_state(_info: StateGeneratorInfo<()>) -> IndexPageState {
greeting: "Hello World!".to_string(),
}
}

pub fn get_template<G: Html>() -> Template<G> {
Template::build("index")
.build_state_fn(get_build_state)
.view_with_state(index_page)
.head_with_state(head)
.build()
}

0 comments on commit 520eebf

Please sign in to comment.