git.delta.rocks / jrsonnet / refs/commits / 9b331948fca2

difftreelog

feat(evaluator) Err instead of panic on unknown variable

Лач2020-06-25parent: #012ef80.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -52,9 +52,11 @@
 		}))
 	}
 
-	pub fn binding(&self, name: &str) -> LazyVal {
-		self.0.bindings.get(name).cloned().unwrap_or_else(|| {
-			panic!("can't find {} in {:?}", name, self);
+	pub fn binding(&self, name: &str) -> Result<LazyVal> {
+		self.0.bindings.get(name).cloned().ok_or_else(|| {
+			create_error::<()>(Error::UnknownVariable(name.to_owned()))
+				.err()
+				.unwrap()
 		})
 	}
 	pub fn into_future(self, ctx: FutureContext) -> Context {
modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/error.rs
1use crate::ValType;2use jsonnet_parser::LocExpr;34#[derive(Debug)]5pub enum Error {6	VariableIsNotDefined(String),7	TypeMismatch(&'static str, Vec<ValType>, ValType),8	NoSuchField(String),910	UnknownFunctionParameter(String),11	BindingParameterASecondTime(String),12	TooManyArgsFunctionHas(usize),13	FunctionParameterNotBoundInCall(String),1415	UndefinedExternalVariable(String),1617	FieldMustBeStringGot(ValType),1819	AttemptedIndexAnArrayWithString(String),20	ValueIndexMustBeTypeGot(ValType, ValType, ValType),21	CantIndexInto(ValType),2223	RuntimeError(String),24	StackOverflow,25	FractionalIndex,26	DivisionByZero,27}2829#[derive(Clone, Debug)]30pub struct StackTraceElement(pub LocExpr, pub String);31#[derive(Debug)]32pub struct StackTrace(pub Vec<StackTraceElement>);3334#[derive(Debug)]35pub struct LocError(pub Error, pub StackTrace);36pub type Result<V> = std::result::Result<V, LocError>;
after · crates/jsonnet-evaluator/src/error.rs
1use crate::ValType;2use jsonnet_parser::LocExpr;34#[derive(Debug)]5pub enum Error {6	VariableIsNotDefined(String),7	TypeMismatch(&'static str, Vec<ValType>, ValType),8	NoSuchField(String),910	UnknownVariable(String),1112	UnknownFunctionParameter(String),13	BindingParameterASecondTime(String),14	TooManyArgsFunctionHas(usize),15	FunctionParameterNotBoundInCall(String),1617	UndefinedExternalVariable(String),1819	FieldMustBeStringGot(ValType),2021	AttemptedIndexAnArrayWithString(String),22	ValueIndexMustBeTypeGot(ValType, ValType, ValType),23	CantIndexInto(ValType),2425	RuntimeError(String),26	StackOverflow,27	FractionalIndex,28	DivisionByZero,29}3031#[derive(Clone, Debug)]32pub struct StackTraceElement(pub LocExpr, pub String);33#[derive(Debug)]34pub struct StackTrace(pub Vec<StackTraceElement>);3536#[derive(Debug)]37pub struct LocError(pub Error, pub StackTrace);38pub type Result<V> = std::result::Result<V, LocError>;
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -397,7 +397,7 @@
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
 		Var(name) => push(locexpr, "var".to_owned(), || {
-			Val::Lazy(context.binding(&name)).unwrap_if_lazy()
+			Val::Lazy(context.binding(&name)?).unwrap_if_lazy()
 		})?,
 		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
 			let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;