difftreelog
feat(evaluator) readable error messages
in: master
6 files changed
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth37base64 = "0.12.3"37base64 = "0.12.3"38rustc-hash = "1.1.0"38rustc-hash = "1.1.0"3940thiserror = "1.0.20"394140# Serialized stdlib42# Serialized stdlib41[dependencies.serde]43[dependencies.serde]crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth2#![allow(clippy::too_many_arguments)]2#![allow(clippy::too_many_arguments)]334use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};4use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val, ValType};5use thiserror::Error;566#[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),101314 #[error("not enough values")]11 NotEnoughValues,15 NotEnoughValues,121617 #[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}1724crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth4};4};5use std::rc::Rc;5use std::rc::Rc;667#[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}1214crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth63 .bindings63 .bindings64 .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 {crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth4};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;788#[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,121516 #[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),152021 #[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,192728 #[error("for loop can only iterate over arrays")]20 InComprehensionCanOnlyIterateOverArray,29 InComprehensionCanOnlyIterateOverArray,213031 #[error("array out of bounds: {0} is not within [0,{1})")]22 ArrayBoundsError(usize, usize),32 ArrayBoundsError(usize, usize),233334 #[error("assert failed: {0}")]24 AssertionFailed(Rc<str>),35 AssertionFailed(Rc<str>),253637 #[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>),294330 UnknownVariable(Rc<str>),44 #[error("only functions can be called, got {0}")]3132 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>),375455 #[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>),405960 #[error("field name should be string, got {0}")]41 FieldMustBeStringGot(ValType),61 FieldMustBeStringGot(ValType),426263 #[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),466970 #[error("super can't be used standalone")]47 StandaloneSuper,71 StandaloneSuper,487273 #[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 },588788 #[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,639697 #[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,67103104 #[error("cant recurse stream manifest")]68 StreamManifestOutputCannotBeRecursed,105 StreamManifestOutputCannotBeRecursed,106 #[error("stream manifest output cannot consist of raw strings")]69 StreamManifestCannotNestString,107 StreamManifestCannotNestString,70108109 #[error("{0}")]71 ImportCallbackError(String),110 ImportCallbackError(String),111 #[error("invalid unicode codepoint: {0}")]72 InvalidUnicodeCodepointGot(u32),112 InvalidUnicodeCodepointGot(u32),73113114 #[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 {crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth86 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 = error91 .trace()91 .trace()92 .092 .0132 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;