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
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -207,7 +207,7 @@
 					}
 					Some(out.into_iter().flatten().flatten().collect())
 				}
-				_ => panic!("for expression evaluated to non-iterable value"),
+				_ => create_error_result(Error::InComprehensionCanOnlyIterateOverArray)?,
 			}
 		}
 	})
@@ -384,13 +384,13 @@
 			context
 				.this()
 				.clone()
-				.unwrap_or_else(|| panic!("this not found")),
+				.ok_or_else(|| create_error(crate::Error::CantUseSelfOutsideOfObject))?,
 		),
 		Literal(LiteralType::Dollar) => Val::Obj(
 			context
 				.dollar()
 				.clone()
-				.unwrap_or_else(|| panic!("dollar not found")),
+				.ok_or_else(|| create_error(crate::Error::NoTopLevelObjectFound))?,
 		),
 		Literal(LiteralType::True) => Val::Bool(true),
 		Literal(LiteralType::False) => Val::Bool(false),
@@ -682,7 +682,7 @@
 										out.reserve(items.len());
 										out.extend(items.iter().cloned());
 									} else {
-										panic!("all array items should be arrays")
+										create_error_result(crate::Error::RuntimeError("in std.join all items should be arrays".into()))?;
 									}
 								}
 
@@ -700,7 +700,7 @@
 										first = false;
 										out += &item;
 									} else {
-										panic!("all array items should be strings")
+										create_error_result(crate::Error::RuntimeError("in std.join all items should be strings".into()))?;
 									}
 								}
 
@@ -753,13 +753,9 @@
 			if assertion_result {
 				evaluate(context, returned)?
 			} else if let Some(msg) = msg {
-				panic!(
-					"assertion failed ({:?}): {}",
-					value,
-					evaluate(context, msg)?.try_cast_str("assertion message should be string")?
-				);
+				create_error_result(crate::Error::AssertionFailed(evaluate(context, msg)?))?
 			} else {
-				panic!("assertion failed ({:?}): no message", value);
+				create_error_result(crate::Error::AssertionFailed(Val::Null))?
 			}
 		}
 		Error(e) => create_error_result(crate::Error::RuntimeError(
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
265 buf.push_str(cur_padding);265 buf.push_str(cur_padding);
266 buf.push('}');266 buf.push('}');
267 }267 }
268 Val::Func(_) | Val::Intristic(_, _) => panic!("tried to manifest function"),268 Val::Func(_) | Val::Intristic(_, _) => create_error_result(Error::RuntimeError("tried to manifest function".into()))?,
269 Val::Lazy(_) => unreachable!(),269 Val::Lazy(_) => unreachable!(),
270 };270 };
271 Ok(())271 Ok(())