Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Able to get the missing variable path from error #546

Merged
merged 6 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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