git.delta.rocks / jrsonnet / refs/commits / 686e5cc6bf11

difftreelog

feat(evaluator) readable error messages

Lach2020-08-26parent: #eaf3cd1.patch.diff
in: master

6 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -37,6 +37,8 @@
 base64 = "0.12.3"
 rustc-hash = "1.1.0"
 
+thiserror = "1.0.20"
+
 # Serialized stdlib
 [dependencies.serde]
 version = "1.0.115"
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -2,16 +2,23 @@
 #![allow(clippy::too_many_arguments)]
 
 use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};
+use thiserror::Error;
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Error)]
 pub enum FormatError {
+	#[error("truncated format code")]
 	TruncatedFormatCode,
+	#[error("unrecognized conversion type: {0}")]
 	UnrecognizedConversionType(char),
 
+	#[error("not enough values")]
 	NotEnoughValues,
 
+	#[error("cannot use * width with object")]
 	CannotUseStarWidthWithObject,
+	#[error("mapping keys required")]
 	MappingKeysRequired,
+	#[error("no such format field: {0}")]
 	NoSuchFormatField(Rc<str>),
 }
 
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -4,9 +4,11 @@
 };
 use std::rc::Rc;
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, thiserror::Error)]
 pub enum SortError {
+	#[error("sort key should be string or number")]
 	SortKeyShouldBeStringOrNumber,
+	#[error("sort elements should have equal types")]
 	SortElementsShouldHaveEqualType,
 }
 
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -63,7 +63,7 @@
 			.bindings
 			.get(&name)
 			.cloned()
-			.ok_or_else(|| UnknownVariable(name))?)
+			.ok_or_else(|| VariableIsNotDefined(name))?)
 	}
 	pub fn into_future(self, ctx: FutureContext) -> Self {
 		{
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
4};4};
5use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};5use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
6use std::{path::PathBuf, rc::Rc};6use std::{path::PathBuf, rc::Rc};
7use thiserror::Error;
78
8#[derive(Debug, Clone)]9#[derive(Error, Debug, Clone)]
9pub enum Error {10pub enum Error {
11 #[error("intrinsic not found: {0}.{1}")]
10 IntrinsicNotFound(Rc<str>, Rc<str>),12 IntrinsicNotFound(Rc<str>, Rc<str>),
13 #[error("argument reordering in intrisics not supported yet")]
11 IntrinsicArgumentReorderingIsNotSupportedYet,14 IntrinsicArgumentReorderingIsNotSupportedYet,
1215
16 #[error("operator {0} does not operate on type {1}")]
13 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),17 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),
18 #[error("binary operation {1} {0} {2} is not implemented")]
14 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),19 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),
1520
21 #[error("no top level object in this context")]
16 NoTopLevelObjectFound,22 NoTopLevelObjectFound,
23 #[error("self is only usable inside objects")]
17 CantUseSelfOutsideOfObject,24 CantUseSelfOutsideOfObject,
25 #[error("super is only usable inside objects")]
18 CantUseSuperOutsideOfObject,26 CantUseSuperOutsideOfObject,
1927
28 #[error("for loop can only iterate over arrays")]
20 InComprehensionCanOnlyIterateOverArray,29 InComprehensionCanOnlyIterateOverArray,
2130
31 #[error("array out of bounds: {0} is not within [0,{1})")]
22 ArrayBoundsError(usize, usize),32 ArrayBoundsError(usize, usize),
2333
34 #[error("assert failed: {0}")]
24 AssertionFailed(Rc<str>),35 AssertionFailed(Rc<str>),
2536
37 #[error("variable is not defined: {0}")]
26 VariableIsNotDefined(String),38 VariableIsNotDefined(Rc<str>),
39 #[error("type mismatch: expected {2}, got {1:?} {0}")]
27 TypeMismatch(&'static str, Vec<ValType>, ValType),40 TypeMismatch(&'static str, Vec<ValType>, ValType),
41 #[error("no such field: {0}")]
28 NoSuchField(Rc<str>),42 NoSuchField(Rc<str>),
2943
30 UnknownVariable(Rc<str>),44 #[error("only functions can be called, got {0}")]
31
32 OnlyFunctionsCanBeCalledGot(ValType),45 OnlyFunctionsCanBeCalledGot(ValType),
46 #[error("parameter {0} is not defined")]
33 UnknownFunctionParameter(String),47 UnknownFunctionParameter(String),
48 #[error("argument {0} is already bound")]
34 BindingParameterASecondTime(Rc<str>),49 BindingParameterASecondTime(Rc<str>),
50 #[error("too many args, function has {0}")]
35 TooManyArgsFunctionHas(usize),51 TooManyArgsFunctionHas(usize),
52 #[error("founction argument is not passed: {0}")]
36 FunctionParameterNotBoundInCall(Rc<str>),53 FunctionParameterNotBoundInCall(Rc<str>),
3754
55 #[error("external variable is not defined: {0}")]
38 UndefinedExternalVariable(Rc<str>),56 UndefinedExternalVariable(Rc<str>),
57 #[error("native is not defined: {0}")]
39 UndefinedExternalFunction(Rc<str>),58 UndefinedExternalFunction(Rc<str>),
4059
60 #[error("field name should be string, got {0}")]
41 FieldMustBeStringGot(ValType),61 FieldMustBeStringGot(ValType),
4262
63 #[error("attempted to index array with string {0}")]
43 AttemptedIndexAnArrayWithString(Rc<str>),64 AttemptedIndexAnArrayWithString(Rc<str>),
65 #[error("{0} index type should be {1}, got {2}")]
44 ValueIndexMustBeTypeGot(ValType, ValType, ValType),66 ValueIndexMustBeTypeGot(ValType, ValType, ValType),
67 #[error("cant index into {0}")]
45 CantIndexInto(ValType),68 CantIndexInto(ValType),
4669
70 #[error("super can't be used standalone")]
47 StandaloneSuper,71 StandaloneSuper,
4872
73 #[error("can't resolve {1} from {0}")]
49 ImportFileNotFound(PathBuf, PathBuf),74 ImportFileNotFound(PathBuf, PathBuf),
75 #[error("resolved file not found: {0}")]
50 ResolvedFileNotFound(PathBuf),76 ResolvedFileNotFound(PathBuf),
77 #[error("imported file is not valid utf-8: {0:?}")]
51 ImportBadFileUtf8(PathBuf),78 ImportBadFileUtf8(PathBuf),
79 #[error("tried to import {1} from {0}, but imports is not supported")]
52 ImportNotSupported(PathBuf, PathBuf),80 ImportNotSupported(PathBuf, PathBuf),
81 #[error("syntax error")]
53 ImportSyntaxError {82 ImportSyntaxError {
54 path: Rc<PathBuf>,83 path: Rc<PathBuf>,
55 source_code: Rc<str>,84 source_code: Rc<str>,
56 error: Box<jrsonnet_parser::ParseError>,85 error: Box<jrsonnet_parser::ParseError>,
57 },86 },
5887
88 #[error("runtime error: {0}")]
59 RuntimeError(Rc<str>),89 RuntimeError(Rc<str>),
90 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
60 StackOverflow,91 StackOverflow,
92 #[error("tried to index by fractional value")]
61 FractionalIndex,93 FractionalIndex,
94 #[error("attempted to divide by zero")]
62 DivisionByZero,95 DivisionByZero,
6396
97 #[error("string manifest output is not an string")]
64 StringManifestOutputIsNotAString,98 StringManifestOutputIsNotAString,
99 #[error("stream manifest output is not an array")]
65 StreamManifestOutputIsNotAArray,100 StreamManifestOutputIsNotAArray,
101 #[error("multi manifest output is not an object")]
66 MultiManifestOutputIsNotAObject,102 MultiManifestOutputIsNotAObject,
67103
104 #[error("cant recurse stream manifest")]
68 StreamManifestOutputCannotBeRecursed,105 StreamManifestOutputCannotBeRecursed,
106 #[error("stream manifest output cannot consist of raw strings")]
69 StreamManifestCannotNestString,107 StreamManifestCannotNestString,
70108
109 #[error("{0}")]
71 ImportCallbackError(String),110 ImportCallbackError(String),
111 #[error("invalid unicode codepoint: {0}")]
72 InvalidUnicodeCodepointGot(u32),112 InvalidUnicodeCodepointGot(u32),
73113
114 #[error("format error: {0}")]
74 Format(FormatError),115 Format(#[from] FormatError),
116 #[error("sort error: {0}")]
75 Sort(SortError),117 Sort(#[from] SortError),
76}118}
77impl From<Error> for LocError {119impl From<Error> for LocError {
78 fn from(e: Error) -> Self {120 fn from(e: Error) -> Self {
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -86,7 +86,7 @@
 		evaluation_state: &EvaluationState,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
-		writeln!(out, "{:?}", error.error())?;
+		writeln!(out, "{}", error.error())?;
 		let file_names = error
 			.trace()
 			.0
@@ -132,7 +132,7 @@
 		evaluation_state: &EvaluationState,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
-		writeln!(out, "{:?}", error.error())?;
+		writeln!(out, "{}", error.error())?;
 		for (i, item) in error.trace().0.iter().enumerate() {
 			if i != 0 {
 				writeln!(out)?;
@@ -171,7 +171,7 @@
 			display_list::{DisplayList, FormatOptions},
 			snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
 		};
-		writeln!(out, "{:?}", error.error())?;
+		writeln!(out, "{}", error.error())?;
 		let trace = &error.trace();
 		for item in trace.0.iter() {
 			let desc = &item.desc;