Skip to content

Commit

Permalink
fix: šŸ› allowed build state to return ErrorCause for incremental genā€¦
Browse files Browse the repository at this point in the history
ā€¦eration
  • Loading branch information
arctic-hen7 committed Aug 19, 2021
1 parent 96066d0 commit dd4d60f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/showcase/app/src/pages/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use perseus::template::Template;
use perseus::errors::ErrorCause;
use serde::{Deserialize, Serialize};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

Expand All @@ -21,7 +22,7 @@ pub fn get_page<G: GenericNode>() -> Template<G> {
.template(template_fn())
}

pub async fn get_static_props(_path: String) -> Result<String, String> {
pub async fn get_static_props(_path: String) -> Result<String, (String, ErrorCause)> {
Ok(serde_json::to_string(&IndexPageProps {
greeting: "Hello World!".to_string(),
})
Expand Down
7 changes: 6 additions & 1 deletion examples/showcase/app/src/pages/post.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use perseus::template::Template;
use perseus::errors::ErrorCause;
use serde::{Deserialize, Serialize};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

Expand Down Expand Up @@ -30,7 +31,11 @@ pub fn get_page<G: GenericNode>() -> Template<G> {
.template(template_fn())
}

pub async fn get_static_props(path: String) -> Result<String, String> {
pub async fn get_static_props(path: String) -> Result<String, (String, ErrorCause)> {
// This path is illegal, and can't be rendered
if path == "post/tests" {
return Err(("illegal page".to_string(), ErrorCause::Client(Some(404))))
}
// This is just an example
let title = urlencoding::decode(&path).unwrap();
let content = format!(
Expand Down
3 changes: 2 additions & 1 deletion examples/showcase/app/src/pages/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use perseus::template::Template;
use perseus::errors::ErrorCause;
use serde::{Deserialize, Serialize};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

Expand All @@ -24,7 +25,7 @@ pub fn get_page<G: GenericNode>() -> Template<G> {
.build_paths_fn(Box::new(get_build_paths))
}

pub async fn get_build_state(_path: String) -> Result<String, String> {
pub async fn get_build_state(_path: String) -> Result<String, (String, ErrorCause)> {
Ok(serde_json::to_string(&TimePageProps {
time: format!("{:?}", std::time::SystemTime::now()),
})
Expand Down
3 changes: 2 additions & 1 deletion examples/showcase/app/src/pages/time_root.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use perseus::template::Template;
use perseus::errors::ErrorCause;
use serde::{Deserialize, Serialize};
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};

Expand All @@ -24,7 +25,7 @@ pub fn get_page<G: GenericNode>() -> Template<G> {
.build_state_fn(Box::new(get_build_state))
}

pub async fn get_build_state(_path: String) -> Result<String, String> {
pub async fn get_build_state(_path: String) -> Result<String, (String, ErrorCause)> {
Ok(serde_json::to_string(&TimePageProps {
time: format!("{:?}", std::time::SystemTime::now()),
})
Expand Down
7 changes: 4 additions & 3 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ macro_rules! make_async_trait {

// A series of asynchronous closure traits that prevent the user from having to pin their functions
make_async_trait!(GetBuildPathsFnType, StringResult<Vec<String>>);
make_async_trait!(GetBuildStateFnType, StringResult<String>, path: String);
// The build state strategy needs an error cause if it's invoked from incremental
make_async_trait!(GetBuildStateFnType, StringResultWithCause<String>, path: String);
make_async_trait!(
GetRequestStateFnType,
StringResultWithCause<String>,
Expand Down Expand Up @@ -192,10 +193,10 @@ impl<G: GenericNode> Template<G> {
let res = get_build_state.call(path).await;
match res {
Ok(res) => Ok(res),
Err(err) => bail!(ErrorKind::RenderFnFailed(
Err((err, cause)) => bail!(ErrorKind::RenderFnFailed(
"get_build_state".to_string(),
self.get_path(),
ErrorCause::Server(None),
cause,
err
)),
}
Expand Down

0 comments on commit dd4d60f

Please sign in to comment.