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
37base64 = "0.12.3"37base64 = "0.12.3"
38rustc-hash = "1.1.0"38rustc-hash = "1.1.0"
39
40thiserror = "1.0.20"
3941
40# Serialized stdlib42# Serialized stdlib
41[dependencies.serde]43[dependencies.serde]
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
2#![allow(clippy::too_many_arguments)]2#![allow(clippy::too_many_arguments)]
33
4use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};4use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};
5use thiserror::Error;
56
6#[derive(Debug, Clone)]7#[derive(Debug, Clone, Error)]
7pub enum FormatError {8pub enum FormatError {
9 #[error("truncated format code")]
8 TruncatedFormatCode,10 TruncatedFormatCode,
11 #[error("unrecognized conversion type: {0}")]
9 UnrecognizedConversionType(char),12 UnrecognizedConversionType(char),
1013
14 #[error("not enough values")]
11 NotEnoughValues,15 NotEnoughValues,
1216
17 #[error("cannot use * width with object")]
13 CannotUseStarWidthWithObject,18 CannotUseStarWidthWithObject,
19 #[error("mapping keys required")]
14 MappingKeysRequired,20 MappingKeysRequired,
21 #[error("no such format field: {0}")]
15 NoSuchFormatField(Rc<str>),22 NoSuchFormatField(Rc<str>),
16}23}
1724
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
4};4};
5use std::rc::Rc;5use std::rc::Rc;
66
7#[derive(Debug, Clone)]7#[derive(Debug, Clone, thiserror::Error)]
8pub enum SortError {8pub enum SortError {
9 #[error("sort key should be string or number")]
9 SortKeyShouldBeStringOrNumber,10 SortKeyShouldBeStringOrNumber,
11 #[error("sort elements should have equal types")]
10 SortElementsShouldHaveEqualType,12 SortElementsShouldHaveEqualType,
11}13}
1214
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
63 .bindings63 .bindings
64 .get(&name)64 .get(&name)
65 .cloned()65 .cloned()
66 .ok_or_else(|| UnknownVariable(name))?)66 .ok_or_else(|| VariableIsNotDefined(name))?)
67 }67 }
68 pub fn into_future(self, ctx: FutureContext) -> Self {68 pub fn into_future(self, ctx: FutureContext) -> Self {
69 {69 {
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
86 evaluation_state: &EvaluationState,86 evaluation_state: &EvaluationState,
87 error: &LocError,87 error: &LocError,
88 ) -> Result<(), std::fmt::Error> {88 ) -> Result<(), std::fmt::Error> {
89 writeln!(out, "{:?}", error.error())?;89 writeln!(out, "{}", error.error())?;
90 let file_names = error90 let file_names = error
91 .trace()91 .trace()
92 .092 .0
132 evaluation_state: &EvaluationState,132 evaluation_state: &EvaluationState,
133 error: &LocError,133 error: &LocError,
134 ) -> Result<(), std::fmt::Error> {134 ) -> Result<(), std::fmt::Error> {
135 writeln!(out, "{:?}", error.error())?;135 writeln!(out, "{}", error.error())?;
136 for (i, item) in error.trace().0.iter().enumerate() {136 for (i, item) in error.trace().0.iter().enumerate() {
137 if i != 0 {137 if i != 0 {
138 writeln!(out)?;138 writeln!(out)?;
171 display_list::{DisplayList, FormatOptions},171 display_list::{DisplayList, FormatOptions},
172 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},172 snippet::{AnnotationType, Slice, Snippet, SourceAnnotation},
173 };173 };
174 writeln!(out, "{:?}", error.error())?;174 writeln!(out, "{}", error.error())?;
175 let trace = &error.trace();175 let trace = &error.trace();
176 for item in trace.0.iter() {176 for item in trace.0.iter() {
177 let desc = &item.desc;177 let desc = &item.desc;