Skip to content

Commit

Permalink
refactor: improve access to nested error in RenderError
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Dec 31, 2023
1 parent 0d30e2d commit d06e071
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
8 changes: 3 additions & 5 deletions examples/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ fn main() -> Result<(), Box<dyn StdError>> {
.render_template("{{err \"db\"}}", &json!({}))
.unwrap_err();
// get nested error to from `RenderError`
match e2.reason() {
Some(RenderErrorReason::NestedError(e)) => {
if let Some(HelperError::DbError) = e.downcast_ref::<HelperError>() {
println!("Detected error from helper: db error",)
}
match e2.source().and_then(|e| e.downcast_ref::<HelperError>()) {
Some(HelperError::DbError) => {
println!("Detected error from helper: db error",)
}
_ => {}
}
Expand Down
52 changes: 43 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use walkdir::Error as WalkdirError;
use rhai::{EvalAltResult, ParseError};

/// Error when rendering data on template.
#[derive(Debug, Default, Error)]
#[derive(Debug, Default)]
pub struct RenderError {
pub desc: String,
pub template_name: Option<String>,
pub line_no: Option<usize>,
pub column_no: Option<usize>,
#[source]
cause: Option<RenderErrorReason>,
unimplemented: bool,
// backtrace: Backtrace,
Expand Down Expand Up @@ -59,7 +58,11 @@ pub enum RenderErrorReason {
#[error("Template not found {0}")]
TemplateNotFound(String),
#[error("Failed to parse template {0}")]
TemplateError(#[from] TemplateError),
TemplateError(
#[from]
#[source]
TemplateError,
),
#[error("Failed to access variable in strict mode {0:?}")]
MissingVariable(Option<String>),
#[error("Partial not found {0}")]
Expand Down Expand Up @@ -89,19 +92,39 @@ pub enum RenderErrorReason {
#[error("Cannot access array/vector with string index, {0}")]
InvalidJsonIndex(String),
#[error("Failed to access JSON data: {0}")]
SerdeError(#[from] SerdeError),
SerdeError(
#[from]
#[source]
SerdeError,
),
#[error("IO Error: {0}")]
IOError(#[from] IOError),
IOError(
#[from]
#[source]
IOError,
),
#[error("FromUtf8Error: {0}")]
Utf8Error(#[from] FromUtf8Error),
Utf8Error(
#[from]
#[source]
FromUtf8Error,
),
#[error("Nested error: {0}")]
NestedError(Box<dyn StdError + Send + Sync + 'static>),
NestedError(#[source] Box<dyn StdError + Send + Sync + 'static>),
#[cfg(feature = "script_helper")]
#[error("Cannot convert data to Rhai dynamic: {0}")]
ScriptValueError(#[from] Box<EvalAltResult>),
ScriptValueError(
#[from]
#[source]
Box<EvalAltResult>,
),
#[cfg(feature = "script_helper")]
#[error("Failed to load rhai script: {0}")]
ScriptLoadError(#[from] ScriptError),
ScriptLoadError(
#[from]
#[source]
ScriptError,
),
#[error("{0}")]
Other(String),
}
Expand Down Expand Up @@ -153,11 +176,22 @@ impl RenderError {
self.unimplemented
}

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

impl StdError for RenderError {
fn description(&self) -> &str {
&self.desc
}

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

/// Template parsing error
#[derive(Debug, Error)]
pub enum TemplateErrorReason {
Expand Down

0 comments on commit d06e071

Please sign in to comment.