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
before · crates/jsonnet-evaluator/src/error.rs
1use crate::ValType;2use jsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};3use std::{path::PathBuf, rc::Rc};45#[derive(Debug, Clone)]6pub enum Error {7	IntristicNotFound(Rc<str>, Rc<str>),8	IntristicArgumentReorderingIsNotSupportedYet,910	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),11	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),1213	VariableIsNotDefined(String),14	TypeMismatch(&'static str, Vec<ValType>, ValType),15	NoSuchField(Rc<str>),1617	UnknownVariable(Rc<str>),1819	OnlyFunctionsCanBeCalledGot(ValType),20	UnknownFunctionParameter(String),21	BindingParameterASecondTime(Rc<str>),22	TooManyArgsFunctionHas(usize),23	FunctionParameterNotBoundInCall(Rc<str>),2425	UndefinedExternalVariable(Rc<str>),2627	FieldMustBeStringGot(ValType),2829	AttemptedIndexAnArrayWithString(Rc<str>),30	ValueIndexMustBeTypeGot(ValType, ValType, ValType),31	CantIndexInto(ValType),3233	StandaloneSuper,3435	ImportFileNotFound(PathBuf, PathBuf),36	ResolvedFileNotFound(PathBuf),37	ImportBadFileUtf8(PathBuf),38	ImportNotSupported(PathBuf, PathBuf),39	ImportSyntaxError(jsonnet_parser::ParseError),4041	RuntimeError(Rc<str>),42	StackOverflow,43	FractionalIndex,44	DivisionByZero,45}4647#[derive(Clone, Debug)]48pub struct StackTraceElement(pub ExprLocation, pub String);49#[derive(Debug, Clone)]50pub struct StackTrace(pub Vec<StackTraceElement>);5152#[derive(Debug, Clone)]53pub struct LocError(pub Error, pub StackTrace);54pub type Result<V> = std::result::Result<V, LocError>;
after · crates/jsonnet-evaluator/src/error.rs
1use crate::{Val, ValType};2use jsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};3use std::{path::PathBuf, rc::Rc};45#[derive(Debug, Clone)]6pub enum Error {7	IntristicNotFound(Rc<str>, Rc<str>),8	IntristicArgumentReorderingIsNotSupportedYet,910	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),11	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),1213	NoTopLevelObjectFound,14	CantUseSelfOutsideOfObject,15	CantUseSuperOutsideOfObject,1617	InComprehensionCanOnlyIterateOverArray,1819	ArrayBoundsError(usize, usize),2021	AssertionFailed(Val),2223	VariableIsNotDefined(String),24	TypeMismatch(&'static str, Vec<ValType>, ValType),25	NoSuchField(Rc<str>),2627	UnknownVariable(Rc<str>),2829	OnlyFunctionsCanBeCalledGot(ValType),30	UnknownFunctionParameter(String),31	BindingParameterASecondTime(Rc<str>),32	TooManyArgsFunctionHas(usize),33	FunctionParameterNotBoundInCall(Rc<str>),3435	UndefinedExternalVariable(Rc<str>),3637	FieldMustBeStringGot(ValType),3839	AttemptedIndexAnArrayWithString(Rc<str>),40	ValueIndexMustBeTypeGot(ValType, ValType, ValType),41	CantIndexInto(ValType),4243	StandaloneSuper,4445	ImportFileNotFound(PathBuf, PathBuf),46	ResolvedFileNotFound(PathBuf),47	ImportBadFileUtf8(PathBuf),48	ImportNotSupported(PathBuf, PathBuf),49	ImportSyntaxError(jsonnet_parser::ParseError),5051	RuntimeError(Rc<str>),52	StackOverflow,53	FractionalIndex,54	DivisionByZero,55}5657#[derive(Clone, Debug)]58pub struct StackTraceElement(pub ExprLocation, pub String);59#[derive(Debug, Clone)]60pub struct StackTrace(pub Vec<StackTraceElement>);6162#[derive(Debug, Clone)]63pub struct LocError(pub Error, pub StackTrace);64pub 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
@@ -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
--- 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(())