Skip to content

Commit dd4d60f

Browse files
committed
fix: šŸ› allowed build state to return ErrorCause for incremental generation
1 parent 96066d0 commit dd4d60f

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

ā€Žexamples/showcase/app/src/pages/index.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use perseus::template::Template;
2+
use perseus::errors::ErrorCause;
23
use serde::{Deserialize, Serialize};
34
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
45

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

24-
pub async fn get_static_props(_path: String) -> Result<String, String> {
25+
pub async fn get_static_props(_path: String) -> Result<String, (String, ErrorCause)> {
2526
Ok(serde_json::to_string(&IndexPageProps {
2627
greeting: "Hello World!".to_string(),
2728
})

ā€Žexamples/showcase/app/src/pages/post.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use perseus::template::Template;
2+
use perseus::errors::ErrorCause;
23
use serde::{Deserialize, Serialize};
34
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
45

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

33-
pub async fn get_static_props(path: String) -> Result<String, String> {
34+
pub async fn get_static_props(path: String) -> Result<String, (String, ErrorCause)> {
35+
// This path is illegal, and can't be rendered
36+
if path == "post/tests" {
37+
return Err(("illegal page".to_string(), ErrorCause::Client(Some(404))))
38+
}
3439
// This is just an example
3540
let title = urlencoding::decode(&path).unwrap();
3641
let content = format!(

ā€Žexamples/showcase/app/src/pages/time.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use perseus::template::Template;
2+
use perseus::errors::ErrorCause;
23
use serde::{Deserialize, Serialize};
34
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
45

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

27-
pub async fn get_build_state(_path: String) -> Result<String, String> {
28+
pub async fn get_build_state(_path: String) -> Result<String, (String, ErrorCause)> {
2829
Ok(serde_json::to_string(&TimePageProps {
2930
time: format!("{:?}", std::time::SystemTime::now()),
3031
})

ā€Žexamples/showcase/app/src/pages/time_root.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use perseus::template::Template;
2+
use perseus::errors::ErrorCause;
23
use serde::{Deserialize, Serialize};
34
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
45

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

27-
pub async fn get_build_state(_path: String) -> Result<String, String> {
28+
pub async fn get_build_state(_path: String) -> Result<String, (String, ErrorCause)> {
2829
Ok(serde_json::to_string(&TimePageProps {
2930
time: format!("{:?}", std::time::SystemTime::now()),
3031
})

ā€Žsrc/template.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ macro_rules! make_async_trait {
8787

8888
// A series of asynchronous closure traits that prevent the user from having to pin their functions
8989
make_async_trait!(GetBuildPathsFnType, StringResult<Vec<String>>);
90-
make_async_trait!(GetBuildStateFnType, StringResult<String>, path: String);
90+
// The build state strategy needs an error cause if it's invoked from incremental
91+
make_async_trait!(GetBuildStateFnType, StringResultWithCause<String>, path: String);
9192
make_async_trait!(
9293
GetRequestStateFnType,
9394
StringResultWithCause<String>,
@@ -192,10 +193,10 @@ impl<G: GenericNode> Template<G> {
192193
let res = get_build_state.call(path).await;
193194
match res {
194195
Ok(res) => Ok(res),
195-
Err(err) => bail!(ErrorKind::RenderFnFailed(
196+
Err((err, cause)) => bail!(ErrorKind::RenderFnFailed(
196197
"get_build_state".to_string(),
197198
self.get_path(),
198-
ErrorCause::Server(None),
199+
cause,
199200
err
200201
)),
201202
}

0 commit comments

Comments
Ā (0)