Skip to content

Commit

Permalink
Merge pull request #546 from linw1995/master
Browse files Browse the repository at this point in the history
Able to get the missing variable path from error
  • Loading branch information
sunng87 committed Nov 21, 2022
2 parents 80687a7 + 832679d commit b70f773
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ impl From<TemplateError> for RenderError {
}
}

/// Template rendering error
#[derive(Debug, Error)]
pub enum RenderErrorReason {
#[error("missing variable path {0:?}")]
MissingVariable(Option<String>),
}

impl From<RenderErrorReason> for RenderError {
fn from(e: RenderErrorReason) -> RenderError {
match e {
RenderErrorReason::MissingVariable(_) => {
RenderError::from_error("Failed to access variable in strict mode.", e)
}
}
}
}

#[cfg(feature = "script_helper")]
impl From<Box<EvalAltResult>> for RenderError {
fn from(e: Box<EvalAltResult>) -> RenderError {
Expand Down Expand Up @@ -102,11 +119,7 @@ impl RenderError {
}

pub fn strict_error(path: Option<&String>) -> RenderError {
let msg = match path {
Some(path) => format!("Variable {:?} not found in strict mode.", path),
None => "Value is missing in strict mode".to_owned(),
};
RenderError::new(msg)
RenderErrorReason::MissingVariable(path.map(|p| p.to_owned())).into()
}

pub fn from_error<E>(error_info: &str, cause: E) -> RenderError
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ extern crate serde_json;
pub use self::block::{BlockContext, BlockParams};
pub use self::context::Context;
pub use self::decorators::DecoratorDef;
pub use self::error::{RenderError, TemplateError};
pub use self::error::{RenderError, RenderErrorReason, TemplateError};
pub use self::helpers::{HelperDef, HelperResult};
pub use self::json::path::Path;
pub use self::json::value::{to_json, JsonRender, PathAndJson, ScopedJson};
Expand Down
23 changes: 22 additions & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,13 +722,14 @@ impl<'reg> Registry<'reg> {
#[cfg(test)]
mod test {
use crate::context::Context;
use crate::error::RenderError;
use crate::error::{RenderError, RenderErrorReason};
use crate::helpers::HelperDef;
use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Helper, RenderContext, Renderable};
use crate::support::str::StringWriter;
use crate::template::Template;
use std::error::Error;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
Expand Down Expand Up @@ -985,6 +986,16 @@ mod test {
.render_template("accessing non-exists key {{the_key_never_exists}}", &data)
.unwrap_err();
assert_eq!(render_error.column_no.unwrap(), 26);
assert_eq!(
render_error
.source()
.and_then(|e| e.downcast_ref::<RenderErrorReason>())
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
})
.unwrap(),
"the_key_never_exists"
);

let data2 = json!([1, 2, 3]);
assert!(r
Expand All @@ -997,6 +1008,16 @@ mod test {
.render_template("accessing invalid array index {{this.[3]}}", &data2)
.unwrap_err();
assert_eq!(render_error2.column_no.unwrap(), 31);
assert_eq!(
render_error2
.source()
.and_then(|e| e.downcast_ref::<RenderErrorReason>())
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
})
.unwrap(),
"this.[3]"
)
}

use crate::json::value::ScopedJson;
Expand Down

0 comments on commit b70f773

Please sign in to comment.