git.delta.rocks / jrsonnet / refs/commits / a2ae6ff1c0dc

difftreelog

source

crates/jrsonnet-evaluator/src/error.rs5.3 KiBsourcehistory
1use crate::{2	builtin::{format::FormatError, sort::SortError},3	typed::TypeLocError,4};5use gc::{Finalize, Trace};6use jrsonnet_interner::IStr;7use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};8use jrsonnet_types::ValType;9use std::{10	path::{Path, PathBuf},11	rc::Rc,12};13use thiserror::Error;1415#[derive(Error, Debug, Clone, Trace, Finalize)]16pub enum Error {17	#[error("intrinsic not found: {0}")]18	IntrinsicNotFound(IStr),19	#[error("argument reordering in intrisics not supported yet")]20	IntrinsicArgumentReorderingIsNotSupportedYet,2122	#[error("operator {0} does not operate on type {1}")]23	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),24	#[error("binary operation {1} {0} {2} is not implemented")]25	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),2627	#[error("no top level object in this context")]28	NoTopLevelObjectFound,29	#[error("self is only usable inside objects")]30	CantUseSelfOutsideOfObject,31	#[error("no super found")]32	NoSuperFound,3334	#[error("for loop can only iterate over arrays")]35	InComprehensionCanOnlyIterateOverArray,3637	#[error("array out of bounds: {0} is not within [0,{1})")]38	ArrayBoundsError(usize, usize),3940	#[error("assert failed: {0}")]41	AssertionFailed(IStr),4243	#[error("variable is not defined: {0}")]44	VariableIsNotDefined(IStr),45	#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]46	TypeMismatch(&'static str, Vec<ValType>, ValType),47	#[error("no such field: {0}")]48	NoSuchField(IStr),4950	#[error("only functions can be called, got {0}")]51	OnlyFunctionsCanBeCalledGot(ValType),52	#[error("parameter {0} is not defined")]53	UnknownFunctionParameter(String),54	#[error("argument {0} is already bound")]55	BindingParameterASecondTime(IStr),56	#[error("too many args, function has {0}")]57	TooManyArgsFunctionHas(usize),58	#[error("founction argument is not passed: {0}")]59	FunctionParameterNotBoundInCall(IStr),6061	#[error("external variable is not defined: {0}")]62	UndefinedExternalVariable(IStr),63	#[error("native is not defined: {0}")]64	UndefinedExternalFunction(IStr),6566	#[error("field name should be string, got {0}")]67	FieldMustBeStringGot(ValType),6869	#[error("attempted to index array with string {0}")]70	AttemptedIndexAnArrayWithString(IStr),71	#[error("{0} index type should be {1}, got {2}")]72	ValueIndexMustBeTypeGot(ValType, ValType, ValType),73	#[error("cant index into {0}")]74	CantIndexInto(ValType),7576	#[error("super can't be used standalone")]77	StandaloneSuper,7879	#[error("can't resolve {1} from {0}")]80	ImportFileNotFound(PathBuf, PathBuf),81	#[error("resolved file not found: {0}")]82	ResolvedFileNotFound(PathBuf),83	#[error("imported file is not valid utf-8: {0:?}")]84	ImportBadFileUtf8(PathBuf),85	#[error("tried to import {1} from {0}, but imports is not supported")]86	ImportNotSupported(PathBuf, PathBuf),87	#[error(88		"syntax error, expected one of {}, got {:?}",89		.error.expected,90		.source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())91	)]92	ImportSyntaxError {93		path: Rc<Path>,94		source_code: IStr,95		#[unsafe_ignore_trace]96		error: Box<jrsonnet_parser::ParseError>,97	},9899	#[error("runtime error: {0}")]100	RuntimeError(IStr),101	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]102	StackOverflow,103	#[error("infinite recursion detected")]104	RecursiveLazyValueEvaluation,105	#[error("tried to index by fractional value")]106	FractionalIndex,107	#[error("attempted to divide by zero")]108	DivisionByZero,109110	#[error("string manifest output is not an string")]111	StringManifestOutputIsNotAString,112	#[error("stream manifest output is not an array")]113	StreamManifestOutputIsNotAArray,114	#[error("multi manifest output is not an object")]115	MultiManifestOutputIsNotAObject,116117	#[error("cant recurse stream manifest")]118	StreamManifestOutputCannotBeRecursed,119	#[error("stream manifest output cannot consist of raw strings")]120	StreamManifestCannotNestString,121122	#[error("{0}")]123	ImportCallbackError(String),124	#[error("invalid unicode codepoint: {0}")]125	InvalidUnicodeCodepointGot(u32),126127	#[error("format error: {0}")]128	Format(#[from] FormatError),129	#[error("type error: {0}")]130	TypeError(TypeLocError),131	#[error("sort error: {0}")]132	Sort(#[from] SortError),133134	#[cfg(feature = "anyhow-error")]135	#[error(transparent)]136	Other(Rc<anyhow::Error>),137}138139#[cfg(feature = "anyhow-error")]140impl From<anyhow::Error> for LocError {141	fn from(e: anyhow::Error) -> Self {142		Self::new(Error::Other(Rc::new(e)))143	}144}145146impl From<Error> for LocError {147	fn from(e: Error) -> Self {148		Self::new(e)149	}150}151152#[derive(Clone, Debug, Trace, Finalize)]153pub struct StackTraceElement {154	pub location: Option<ExprLocation>,155	pub desc: String,156}157#[derive(Debug, Clone, Trace, Finalize)]158pub struct StackTrace(pub Vec<StackTraceElement>);159160#[derive(Debug, Clone, Trace, Finalize)]161pub struct LocError(Box<(Error, StackTrace)>);162impl LocError {163	pub fn new(e: Error) -> Self {164		Self(Box::new((e, StackTrace(vec![]))))165	}166167	pub const fn error(&self) -> &Error {168		&(self.0).0169	}170	pub fn error_mut(&mut self) -> &mut Error {171		&mut (self.0).0172	}173	pub const fn trace(&self) -> &StackTrace {174		&(self.0).1175	}176	pub fn trace_mut(&mut self) -> &mut StackTrace {177		&mut (self.0).1178	}179}180181pub type Result<V> = std::result::Result<V, LocError>;182183#[macro_export]184macro_rules! throw {185	($e: expr) => {186		return Err($e.into());187	};188}