Skip to content

Commit

Permalink
Merge pull request #571 from sunng87/feat/raise-error-when-partial-no…
Browse files Browse the repository at this point in the history
…t-found

feat: raise an render error when partial not found
  • Loading branch information
sunng87 authored Apr 13, 2023
2 parents 125ff6d + bfe4048 commit 9ae3c5e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ impl From<TemplateError> for RenderError {
pub enum RenderErrorReason {
#[error("missing variable path {0:?}")]
MissingVariable(Option<String>),
#[error("partial not found {0}")]
PartialNotFound(String),
}

impl From<RenderErrorReason> for RenderError {
Expand All @@ -85,6 +87,9 @@ impl From<RenderErrorReason> for RenderError {
RenderErrorReason::MissingVariable(_) => {
RenderError::from_error("Failed to access variable in strict mode.", e)
}
RenderErrorReason::PartialNotFound(_) => {
RenderError::from_error("Partial not found.", e)
}
}
}
}
Expand Down
40 changes: 24 additions & 16 deletions src/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Decorator, Evaluable, RenderContext, Renderable};
use crate::template::Template;
use crate::RenderErrorReason;

pub(crate) const PARTIAL_BLOCK: &str = "@partial-block";

Expand Down Expand Up @@ -147,7 +148,7 @@ pub fn expand_partial<'reg: 'rc, 'rc>(

result
} else {
Ok(())
Err(RenderErrorReason::PartialNotFound(tname.to_owned()).into())
}
}

Expand Down Expand Up @@ -663,23 +664,30 @@ outer third line"#,
hb2.render("t1", &()).unwrap()
)
}
}

#[test]
fn test_issue_534() {
let t1 = "{{title}}";
let t2 = "{{#each modules}}{{> (lookup this \"module\") content name=0}}{{/each}}";
#[test]
fn test_issue_534() {
let t1 = "{{title}}";
let t2 = "{{#each modules}}{{> (lookup this \"module\") content name=0}}{{/each}}";

let data = json!({
"modules": [
{"module": "t1", "content": {"title": "foo"}},
{"module": "t1", "content": {"title": "bar"}},
]
});

let data = json!({
"modules": [
{"module": "t1", "content": {"title": "foo"}},
{"module": "t1", "content": {"title": "bar"}},
]
});
let mut hbs = Registry::new();
hbs.register_template_string("t1", t1).unwrap();
hbs.register_template_string("t2", t2).unwrap();

let mut hbs = Registry::new();
hbs.register_template_string("t1", t1).unwrap();
hbs.register_template_string("t2", t2).unwrap();
assert_eq!("foobar", hbs.render("t2", &data).unwrap());
}

assert_eq!("foobar", hbs.render("t2", &data).unwrap());
#[test]
fn test_partial_not_found() {
let t1 = "{{> bar}}";
let hbs = Registry::new();
assert!(hbs.render_template(&t1, &()).is_err());
}
}
2 changes: 2 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ mod test {
.and_then(|e| e.downcast_ref::<RenderErrorReason>())
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
_ => unreachable!(),
})
.unwrap(),
"the_key_never_exists"
Expand All @@ -1034,6 +1035,7 @@ mod test {
.and_then(|e| e.downcast_ref::<RenderErrorReason>())
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
_ => unreachable!(),
})
.unwrap(),
"this.[3]"
Expand Down

0 comments on commit 9ae3c5e

Please sign in to comment.