git.delta.rocks / jrsonnet / refs/commits / d4ade76b40aa

difftreelog

fix(evaluator) handle rest of panics as errors

Лач2020-06-27parent: #7eb3214.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -1,4 +1,4 @@
-use crate::ValType;
+use crate::{Val, ValType};
 use jsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
 use std::{path::PathBuf, rc::Rc};
 
@@ -10,6 +10,16 @@
 	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),
 	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),
 
+	NoTopLevelObjectFound,
+	CantUseSelfOutsideOfObject,
+	CantUseSuperOutsideOfObject,
+
+	InComprehensionCanOnlyIterateOverArray,
+
+	ArrayBoundsError(usize, usize),
+
+	AssertionFailed(Val),
+
 	VariableIsNotDefined(String),
 	TypeMismatch(&'static str, Vec<ValType>, ValType),
 	NoSuchField(Rc<str>),
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
207 }207 }
208 Some(out.into_iter().flatten().flatten().collect())208 Some(out.into_iter().flatten().flatten().collect())
209 }209 }
210 _ => panic!("for expression evaluated to non-iterable value"),210 _ => create_error_result(Error::InComprehensionCanOnlyIterateOverArray)?,
211 }211 }
212 }212 }
213 })213 })
384 context384 context
385 .this()385 .this()
386 .clone()386 .clone()
387 .unwrap_or_else(|| panic!("this not found")),387 .ok_or_else(|| create_error(crate::Error::CantUseSelfOutsideOfObject))?,
388 ),388 ),
389 Literal(LiteralType::Dollar) => Val::Obj(389 Literal(LiteralType::Dollar) => Val::Obj(
390 context390 context
391 .dollar()391 .dollar()
392 .clone()392 .clone()
393 .unwrap_or_else(|| panic!("dollar not found")),393 .ok_or_else(|| create_error(crate::Error::NoTopLevelObjectFound))?,
394 ),394 ),
395 Literal(LiteralType::True) => Val::Bool(true),395 Literal(LiteralType::True) => Val::Bool(true),
396 Literal(LiteralType::False) => Val::Bool(false),396 Literal(LiteralType::False) => Val::Bool(false),
682 out.reserve(items.len());682 out.reserve(items.len());
683 out.extend(items.iter().cloned());683 out.extend(items.iter().cloned());
684 } else {684 } else {
685 panic!("all array items should be arrays")685 create_error_result(crate::Error::RuntimeError("in std.join all items should be arrays".into()))?;
686 }686 }
687 }687 }
688688
700 first = false;700 first = false;
701 out += &item;701 out += &item;
702 } else {702 } else {
703 panic!("all array items should be strings")703 create_error_result(crate::Error::RuntimeError("in std.join all items should be strings".into()))?;
704 }704 }
705 }705 }
706706
753 if assertion_result {753 if assertion_result {
754 evaluate(context, returned)?754 evaluate(context, returned)?
755 } else if let Some(msg) = msg {755 } else if let Some(msg) = msg {
756 panic!(756 create_error_result(crate::Error::AssertionFailed(evaluate(context, msg)?))?
757 "assertion failed ({:?}): {}",
758 value,
759 evaluate(context, msg)?.try_cast_str("assertion message should be string")?
760 );
761 } else {757 } else {
762 panic!("assertion failed ({:?}): no message", value);758 create_error_result(crate::Error::AssertionFailed(Val::Null))?
763 }759 }
764 }760 }
765 Error(e) => create_error_result(crate::Error::RuntimeError(761 Error(e) => create_error_result(crate::Error::RuntimeError(
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -265,7 +265,7 @@
 			buf.push_str(cur_padding);
 			buf.push('}');
 		}
-		Val::Func(_) | Val::Intristic(_, _) => panic!("tried to manifest function"),
+		Val::Func(_) | Val::Intristic(_, _) => create_error_result(Error::RuntimeError("tried to manifest function".into()))?,
 		Val::Lazy(_) => unreachable!(),
 	};
 	Ok(())