difftreelog
feat(evaluator) readable error messages
in: master
6 files changed
crates/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"
crates/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>),
}
crates/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,
}
crates/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 {
{
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth1use crate::{2 builtin::{format::FormatError, sort::SortError},3 ValType,4};5use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};6use std::{path::PathBuf, rc::Rc};78#[derive(Debug, Clone)]9pub enum Error {10 IntrinsicNotFound(Rc<str>, Rc<str>),11 IntrinsicArgumentReorderingIsNotSupportedYet,1213 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),14 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),1516 NoTopLevelObjectFound,17 CantUseSelfOutsideOfObject,18 CantUseSuperOutsideOfObject,1920 InComprehensionCanOnlyIterateOverArray,2122 ArrayBoundsError(usize, usize),2324 AssertionFailed(Rc<str>),2526 VariableIsNotDefined(String),27 TypeMismatch(&'static str, Vec<ValType>, ValType),28 NoSuchField(Rc<str>),2930 UnknownVariable(Rc<str>),3132 OnlyFunctionsCanBeCalledGot(ValType),33 UnknownFunctionParameter(String),34 BindingParameterASecondTime(Rc<str>),35 TooManyArgsFunctionHas(usize),36 FunctionParameterNotBoundInCall(Rc<str>),3738 UndefinedExternalVariable(Rc<str>),39 UndefinedExternalFunction(Rc<str>),4041 FieldMustBeStringGot(ValType),4243 AttemptedIndexAnArrayWithString(Rc<str>),44 ValueIndexMustBeTypeGot(ValType, ValType, ValType),45 CantIndexInto(ValType),4647 StandaloneSuper,4849 ImportFileNotFound(PathBuf, PathBuf),50 ResolvedFileNotFound(PathBuf),51 ImportBadFileUtf8(PathBuf),52 ImportNotSupported(PathBuf, PathBuf),53 ImportSyntaxError {54 path: Rc<PathBuf>,55 source_code: Rc<str>,56 error: Box<jrsonnet_parser::ParseError>,57 },5859 RuntimeError(Rc<str>),60 StackOverflow,61 FractionalIndex,62 DivisionByZero,6364 StringManifestOutputIsNotAString,65 StreamManifestOutputIsNotAArray,66 MultiManifestOutputIsNotAObject,6768 StreamManifestOutputCannotBeRecursed,69 StreamManifestCannotNestString,7071 ImportCallbackError(String),72 InvalidUnicodeCodepointGot(u32),7374 Format(FormatError),75 Sort(SortError),76}77impl From<Error> for LocError {78 fn from(e: Error) -> Self {79 Self::new(e)80 }81}8283#[derive(Clone, Debug)]84pub struct StackTraceElement {85 pub location: ExprLocation,86 pub desc: String,87}88#[derive(Debug, Clone)]89pub struct StackTrace(pub Vec<StackTraceElement>);9091#[derive(Debug, Clone)]92pub struct LocError(Box<(Error, StackTrace)>);93impl LocError {94 pub fn new(e: Error) -> Self {95 Self(Box::new((e, StackTrace(vec![]))))96 }9798 pub const fn error(&self) -> &Error {99 &(self.0).0100 }101 pub const fn trace(&self) -> &StackTrace {102 &(self.0).1103 }104 pub fn trace_mut(&mut self) -> &mut StackTrace {105 &mut (self.0).1106 }107}108109pub type Result<V> = std::result::Result<V, LocError>;110111#[macro_export]112macro_rules! throw {113 ($e: expr) => {114 return Err($e.into());115 };116}1use crate::{2 builtin::{format::FormatError, sort::SortError},3 ValType,4};5use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};6use std::{path::PathBuf, rc::Rc};7use thiserror::Error;89#[derive(Error, Debug, Clone)]10pub enum Error {11 #[error("intrinsic not found: {0}.{1}")]12 IntrinsicNotFound(Rc<str>, Rc<str>),13 #[error("argument reordering in intrisics not supported yet")]14 IntrinsicArgumentReorderingIsNotSupportedYet,1516 #[error("operator {0} does not operate on type {1}")]17 UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),18 #[error("binary operation {1} {0} {2} is not implemented")]19 BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2021 #[error("no top level object in this context")]22 NoTopLevelObjectFound,23 #[error("self is only usable inside objects")]24 CantUseSelfOutsideOfObject,25 #[error("super is only usable inside objects")]26 CantUseSuperOutsideOfObject,2728 #[error("for loop can only iterate over arrays")]29 InComprehensionCanOnlyIterateOverArray,3031 #[error("array out of bounds: {0} is not within [0,{1})")]32 ArrayBoundsError(usize, usize),3334 #[error("assert failed: {0}")]35 AssertionFailed(Rc<str>),3637 #[error("variable is not defined: {0}")]38 VariableIsNotDefined(Rc<str>),39 #[error("type mismatch: expected {2}, got {1:?} {0}")]40 TypeMismatch(&'static str, Vec<ValType>, ValType),41 #[error("no such field: {0}")]42 NoSuchField(Rc<str>),4344 #[error("only functions can be called, got {0}")]45 OnlyFunctionsCanBeCalledGot(ValType),46 #[error("parameter {0} is not defined")]47 UnknownFunctionParameter(String),48 #[error("argument {0} is already bound")]49 BindingParameterASecondTime(Rc<str>),50 #[error("too many args, function has {0}")]51 TooManyArgsFunctionHas(usize),52 #[error("founction argument is not passed: {0}")]53 FunctionParameterNotBoundInCall(Rc<str>),5455 #[error("external variable is not defined: {0}")]56 UndefinedExternalVariable(Rc<str>),57 #[error("native is not defined: {0}")]58 UndefinedExternalFunction(Rc<str>),5960 #[error("field name should be string, got {0}")]61 FieldMustBeStringGot(ValType),6263 #[error("attempted to index array with string {0}")]64 AttemptedIndexAnArrayWithString(Rc<str>),65 #[error("{0} index type should be {1}, got {2}")]66 ValueIndexMustBeTypeGot(ValType, ValType, ValType),67 #[error("cant index into {0}")]68 CantIndexInto(ValType),6970 #[error("super can't be used standalone")]71 StandaloneSuper,7273 #[error("can't resolve {1} from {0}")]74 ImportFileNotFound(PathBuf, PathBuf),75 #[error("resolved file not found: {0}")]76 ResolvedFileNotFound(PathBuf),77 #[error("imported file is not valid utf-8: {0:?}")]78 ImportBadFileUtf8(PathBuf),79 #[error("tried to import {1} from {0}, but imports is not supported")]80 ImportNotSupported(PathBuf, PathBuf),81 #[error("syntax error")]82 ImportSyntaxError {83 path: Rc<PathBuf>,84 source_code: Rc<str>,85 error: Box<jrsonnet_parser::ParseError>,86 },8788 #[error("runtime error: {0}")]89 RuntimeError(Rc<str>),90 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]91 StackOverflow,92 #[error("tried to index by fractional value")]93 FractionalIndex,94 #[error("attempted to divide by zero")]95 DivisionByZero,9697 #[error("string manifest output is not an string")]98 StringManifestOutputIsNotAString,99 #[error("stream manifest output is not an array")]100 StreamManifestOutputIsNotAArray,101 #[error("multi manifest output is not an object")]102 MultiManifestOutputIsNotAObject,103104 #[error("cant recurse stream manifest")]105 StreamManifestOutputCannotBeRecursed,106 #[error("stream manifest output cannot consist of raw strings")]107 StreamManifestCannotNestString,108109 #[error("{0}")]110 ImportCallbackError(String),111 #[error("invalid unicode codepoint: {0}")]112 InvalidUnicodeCodepointGot(u32),113114 #[error("format error: {0}")]115 Format(#[from] FormatError),116 #[error("sort error: {0}")]117 Sort(#[from] SortError),118}119impl From<Error> for LocError {120 fn from(e: Error) -> Self {121 Self::new(e)122 }123}124125#[derive(Clone, Debug)]126pub struct StackTraceElement {127 pub location: ExprLocation,128 pub desc: String,129}130#[derive(Debug, Clone)]131pub struct StackTrace(pub Vec<StackTraceElement>);132133#[derive(Debug, Clone)]134pub struct LocError(Box<(Error, StackTrace)>);135impl LocError {136 pub fn new(e: Error) -> Self {137 Self(Box::new((e, StackTrace(vec![]))))138 }139140 pub const fn error(&self) -> &Error {141 &(self.0).0142 }143 pub const fn trace(&self) -> &StackTrace {144 &(self.0).1145 }146 pub fn trace_mut(&mut self) -> &mut StackTrace {147 &mut (self.0).1148 }149}150151pub type Result<V> = std::result::Result<V, LocError>;152153#[macro_export]154macro_rules! throw {155 ($e: expr) => {156 return Err($e.into());157 };158}crates/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;