Skip to content

Commit

Permalink
fix(error): Improve error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Nov 17, 2018
1 parent bd2c71c commit e372470
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
3 changes: 0 additions & 3 deletions liquid-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ const ERROR_DESCRIPTION: &str = "liquid";
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{}: {}", ERROR_DESCRIPTION, self.inner.msg)?;
if let Some(ref cause) = self.inner.cause {
writeln!(f, "cause: {}", cause)?;
}
for trace in &self.inner.user_backtrace {
if let Some(trace) = trace.get_trace() {
writeln!(f, "from: {}", trace)?;
Expand Down
8 changes: 5 additions & 3 deletions liquid-interpreter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ impl<'g> Stack<'g> {
let mut indexes = path.iter();
let key = indexes
.next()
.ok_or_else(|| Error::with_msg("No index provided"))?;
.ok_or_else(|| Error::with_msg("No variable provided"))?;
let key = key.to_str();
let value = self.get_root(key.as_ref())?;

indexes.fold(Ok(value), |value, index| {
let value = value?;
let child = value.get(index);
let child = child.ok_or_else(|| {
Error::with_msg("Invalid index").context("index", key.as_ref().to_owned())
Error::with_msg("Unknown index")
.context("variable", format!("{}", path))
.context("index", format!("{}", index))
})?;
Ok(child)
})
Expand All @@ -190,7 +192,7 @@ impl<'g> Stack<'g> {
}
}
self.globals
.ok_or_else(|| Error::with_msg("Invalid index").context("index", name.to_owned()))
.ok_or_else(|| Error::with_msg("Unknown variable").context("variable", name.to_owned()))
.and_then(|g| g.get(name))
.or_else(|err| self.get_index(name).ok_or_else(|| err))
}
Expand Down
1 change: 1 addition & 0 deletions liquid-interpreter/src/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl FilterChain {
entry = f
.filter(&entry, &*arguments)
.chain("Filter error")
.context_with(|| ("filter".to_owned(), format!("{}", self)))
.context_with(|| ("input".to_owned(), format!("{}", &entry)))
.context_with(|| ("args".to_owned(), itertools::join(&arguments, ", ")))?;
}
Expand Down
2 changes: 1 addition & 1 deletion liquid-interpreter/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub trait Globals: fmt::Debug {
impl Globals for Object {
fn get<'a>(&'a self, name: &str) -> Result<&'a Value> {
self.get(name)
.ok_or_else(|| Error::with_msg("Invalid index").context("index", name.to_owned()))
.ok_or_else(|| Error::with_msg("Unknown variable").context("variable", name.to_owned()))
}
}
2 changes: 1 addition & 1 deletion src/filters/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn abs(input: &Value, args: &[Value]) -> FilterResult {
.to_integer()
.map(|i| Value::scalar(i.abs()))
.or_else(|| s.to_float().map(|i| Value::scalar(i.abs())))
.ok_or_else(|| invalid_input("Numeric value expected")),
.ok_or_else(|| invalid_input("Numeric expected")),
_ => Err(invalid_input("Number expected")),
}
}
Expand Down

0 comments on commit e372470

Please sign in to comment.