Skip to content

Commit

Permalink
refactor: remove option on RenderErrorReason
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Dec 31, 2023
1 parent b7fabb4 commit 4cf48a9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 43 deletions.
40 changes: 16 additions & 24 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@ use walkdir::Error as WalkdirError;
use rhai::{EvalAltResult, ParseError};

/// Error when rendering data on template.
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct RenderError {
pub template_name: Option<String>,
pub line_no: Option<usize>,
pub column_no: Option<usize>,
cause: Option<Box<RenderErrorReason>>,
cause: Box<RenderErrorReason>,
unimplemented: bool,
// backtrace: Backtrace,
}

impl fmt::Display for RenderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let desc = self
.cause
.as_ref()
.map(|e| e.to_string())
.unwrap_or_else(|| "".to_owned());
let desc = self.cause.to_string();

match (self.line_no, self.column_no) {
(Some(line), Some(col)) => write!(
f,
Expand Down Expand Up @@ -135,15 +132,20 @@ pub enum RenderErrorReason {
#[source]
ScriptError,
),
#[error("Unimplemented")]
Unimplemented,
#[error("{0}")]
Other(String),
}

impl From<RenderErrorReason> for RenderError {
fn from(e: RenderErrorReason) -> RenderError {
RenderError {
cause: Some(Box::new(e)),
..Default::default()
template_name: None,
line_no: None,
column_no: None,
cause: Box::new(e),
unimplemented: false,
}
}
}
Expand All @@ -154,13 +156,6 @@ impl RenderError {
RenderErrorReason::Other(desc.as_ref().to_string()).into()
}

pub(crate) fn unimplemented() -> RenderError {
RenderError {
unimplemented: true,
..Default::default()
}
}

pub fn strict_error(path: Option<&String>) -> RenderError {
RenderErrorReason::MissingVariable(path.map(|p| p.to_owned())).into()
}
Expand All @@ -170,26 +165,23 @@ impl RenderError {
where
E: StdError + Send + Sync + 'static,
{
RenderError {
cause: Some(Box::new(RenderErrorReason::NestedError(Box::new(cause)))),
..Default::default()
}
RenderErrorReason::NestedError(Box::new(cause)).into()
}

#[inline]
pub(crate) fn is_unimplemented(&self) -> bool {
self.unimplemented
matches!(*self.cause, RenderErrorReason::Unimplemented)
}

/// Get `RenderErrorReason` for this error
pub fn reason(&self) -> Option<&RenderErrorReason> {
self.cause.as_ref().map(|e| e.as_ref())
pub fn reason(&self) -> &RenderErrorReason {
self.cause.as_ref()
}
}

impl StdError for RenderError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.reason().and_then(|e| e.source())
Some(self.reason())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::context::Context;
use crate::error::RenderError;
use crate::error::{RenderError, RenderErrorReason};
use crate::json::value::ScopedJson;
use crate::output::Output;
use crate::registry::Registry;
Expand Down Expand Up @@ -95,7 +95,7 @@ pub trait HelperDef {
_: &'rc Context,
_: &mut RenderContext<'reg, 'rc>,
) -> Result<ScopedJson<'rc>, RenderError> {
Err(RenderError::unimplemented())
Err(RenderErrorReason::Unimplemented.into())
}

/// A complex version of helper interface.
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/string_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,13 @@ mod tests {
#[test]
fn test_invalid_input() {
use crate::error::RenderErrorReason;
use std::error::Error;

let hbs = crate::registry::Registry::new();
let err = hbs
.render_template("{{snakeCase 1}}", &json!({}))
.unwrap_err();
assert!(matches!(
err.reason().unwrap(),
err.reason(),
RenderErrorReason::ParamTypeMismatchForName(_, _, _)
));
}
Expand Down
22 changes: 8 additions & 14 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,13 +1159,10 @@ mod test {
.unwrap_err();
assert_eq!(render_error.column_no.unwrap(), 26);
assert_eq!(
render_error
.reason()
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
_ => unreachable!(),
})
.unwrap(),
match render_error.reason() {
RenderErrorReason::MissingVariable(path) => path.as_ref().unwrap(),
_ => unreachable!(),
},
"the_key_never_exists"
);

Expand All @@ -1181,13 +1178,10 @@ mod test {
.unwrap_err();
assert_eq!(render_error2.column_no.unwrap(), 31);
assert_eq!(
render_error2
.reason()
.and_then(|e| match e {
RenderErrorReason::MissingVariable(path) => path.to_owned(),
_ => unreachable!(),
})
.unwrap(),
match render_error2.reason() {
RenderErrorReason::MissingVariable(path) => path.as_ref().unwrap(),
_ => unreachable!(),
},
"this.[3]"
)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/subexpression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn invalid_json_path() {
let hbs = Handlebars::new();

let error = hbs.render_template("{{x[]@this}}", &data).unwrap_err();
let cause = error.reason().unwrap();
let cause = error.reason();

assert!(matches!(cause, RenderErrorReason::HelperNotFound(_)));
}
Expand Down

0 comments on commit 4cf48a9

Please sign in to comment.