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

difftreelog

source

crates/jrsonnet-evaluator/src/error.rs10.4 KiBsourcehistory
1use std::{cmp::Ordering, convert::Infallible, fmt};23use jrsonnet_gcmodule::{Acyclic, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_ir::{BinaryOpType, Source, SourcePath, Span, Spanned, UnaryOpType};6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{10	function::{CallLocation, FunctionSignature, ParamName},11	stdlib::format::FormatError,12	typed::TypeLocError,13	val::ConvertNumValueError,14	ObjValue, ResolvePathOwned,15};1617pub(crate) fn format_found(list: &[IStr], what: &str) -> String {18	if list.is_empty() {19		return String::new();20	}21	let mut out = String::new();22	out.push_str("\nThere ");23	if list.len() > 1 {24		out.push_str("are ");25	} else {26		out.push_str("is a ");27	}28	out.push_str(what);29	if list.len() > 1 {30		out.push('s');31	}32	out.push_str(" with similar name");33	if list.len() > 1 {34		out.push('s');35	}36	out.push_str(" present: ");37	for (i, v) in list.iter().enumerate() {38		if i != 0 {39			out.push_str(", ");40		}41		out.push_str(v as &str);42	}43	out44}4546const fn format_empty_str(str: &str) -> &str {47	if str.is_empty() {48		"\"\" (empty string)"49	} else {50		str51	}52}5354pub(crate) fn suggest_object_fields(v: &ObjValue, key: IStr) -> Vec<IStr> {55	let mut heap = Vec::new();56	for field in v.fields_ex(57		true,58		#[cfg(feature = "exp-preserve-order")]59		false,60	) {61		let conf = strsim::jaro_winkler(field.as_str(), key.as_str());62		if conf < 0.8 {63			continue;64		}65		assert!(field.as_str() != key.as_str(), "looks like string pooling failure, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");6667		heap.push((conf, field));68	}69	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));70	heap.into_iter().map(|v| v.1).collect()71}7273/// Possible errors74#[allow(missing_docs)]75#[derive(Error, Debug, Clone, Trace)]76#[non_exhaustive]77pub enum ErrorKind {78	#[error("intrinsic not found: {0}")]79	IntrinsicNotFound(IStr),8081	#[error("operator {0} does not operate on type {1}")]82	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),83	#[error("binary operation {1} {0} {2} is not implemented")]84	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),8586	#[error("self/super/$ are only usable inside objects")]87	CantUseSelfSupOutsideOfObject,88	#[error("no super found")]89	NoSuperFound,9091	#[error("for loop can only iterate over arrays")]92	InComprehensionCanOnlyIterateOverArray,9394	#[error("array out of bounds: {0} is not within [0,{1})")]95	ArrayBoundsError(isize, usize),96	#[error("string out of bounds: {0} is not within [0,{1})")]97	StringBoundsError(usize, usize),9899	#[error("assert failed: {}", format_empty_str(.0))]100	AssertionFailed(IStr),101102	#[error("local is not defined: {0}{found}", found = format_found(.1, "local"))]103	VariableIsNotDefined(IStr, Vec<IStr>),104	#[error("duplicate local var: {0}")]105	DuplicateLocalVar(IStr),106107	#[error("type mismatch: expected {expected}, got {2} {0}", expected = .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]108	TypeMismatch(&'static str, Vec<ValType>, ValType),109	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]110	NoSuchField(IStr, Vec<IStr>),111112	#[error("only functions can be called, got {0}")]113	OnlyFunctionsCanBeCalledGot(ValType),114	#[error("parameter {0} is not defined")]115	UnknownFunctionParameter(IStr),116	#[error("argument {0} is already bound")]117	BindingParameterASecondTime(IStr),118	#[error("too many args, function has {0}\nFunction has the following signature: {1}")]119	TooManyArgsFunctionHas(usize, FunctionSignature),120	#[error("function argument is not passed: {0}\nFunction has the following signature: {1}")]121	FunctionParameterNotBoundInCall(ParamName, FunctionSignature),122123	#[error("external variable is not defined: {0}")]124	UndefinedExternalVariable(IStr),125126	#[error("field name should be string, got {0}")]127	FieldMustBeStringGot(ValType),128	#[error("duplicate field name: {}", format_empty_str(.0))]129	DuplicateFieldName(IStr),130131	#[error("attempted to index array with string {}", format_empty_str(.0))]132	AttemptedIndexAnArrayWithString(IStr),133	#[error("{0} index type should be {1}, got {2}")]134	ValueIndexMustBeTypeGot(ValType, ValType, ValType),135	#[error("cant index into {0}")]136	CantIndexInto(ValType),137	#[error("{0} is not indexable")]138	ValueIsNotIndexable(ValType),139140	#[error("super can't be used standalone")]141	StandaloneSuper,142143	#[error("can't resolve {1} from {0}")]144	ImportFileNotFound(SourcePath, ResolvePathOwned),145	#[error("resolved file not found: {:?}", .0)]146	ResolvedFileNotFound(SourcePath),147	#[error("can't import {0}: is a directory")]148	ImportIsADirectory(SourcePath),149	#[error("imported file is not valid utf-8: {0:?}")]150	ImportBadFileUtf8(SourcePath),151	#[error("import io error: {0}")]152	ImportIo(String),153	#[error("tried to import {1} from {0}, but imports are not supported")]154	ImportNotSupported(SourcePath, ResolvePathOwned),155	#[error("can't import from virtual file")]156	CantImportFromVirtualFile,157	#[cfg(not(feature = "ir-parser"))]158	#[error(159		"syntax error: {}",160		// Peg has no fancier way to handle critical parsing errors https://github.com/kevinmehall/rust-peg/issues/225161		{.error.expected.tokens().find(|t| t.starts_with("!!!")).map_or_else(|| {162			format!(163				"expected {}, got {:?}",164				.error.expected,165				.path.code().chars().nth(error.location.offset)166				.map_or_else(|| "EOF".into(), |c| c.to_string())167			)168		}, |v| v[3..].into())}169	)]170	ImportSyntaxError {171		path: Source,172		#[trace(skip)]173		error: Box<jrsonnet_peg_parser::ParseError>,174	},175176	#[cfg(feature = "ir-parser")]177	#[error("syntax error: {error}")]178	ImportSyntaxError {179		path: Source,180		#[trace(skip)]181		error: Box<jrsonnet_ir_parser::ParseError>,182	},183184	#[error("runtime error: {}", format_empty_str(.0))]185	RuntimeError(IStr),186	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]187	StackOverflow,188	#[error("infinite recursion detected")]189	InfiniteRecursionDetected,190	#[error("tried to index by fractional value")]191	FractionalIndex,192	#[error("attempted to divide by zero")]193	DivisionByZero,194195	#[error("string manifest output is not an string")]196	StringManifestOutputIsNotAString,197	#[error("stream manifest output is not an array")]198	StreamManifestOutputIsNotAArray,199	#[error("multi manifest output is not an object")]200	MultiManifestOutputIsNotAObject,201202	#[error("cant recurse stream manifest")]203	StreamManifestOutputCannotBeRecursed,204	#[error("stream manifest output cannot consist of raw strings")]205	StreamManifestCannotNestString,206207	#[error("{}", format_empty_str(.0))]208	ImportCallbackError(String),209	#[error("invalid unicode codepoint: {0}")]210	InvalidUnicodeCodepointGot(u32),211212	#[error("convert num value: {0}")]213	ConvertNumValue(#[from] ConvertNumValueError),214215	#[error("format error: {0}")]216	Format(#[from] FormatError),217	#[error("type error: {0}")]218	TypeError(TypeLocError),219220	#[cfg(feature = "anyhow-error")]221	#[error(transparent)]222	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),223}224225#[cfg(feature = "anyhow-error")]226impl From<anyhow::Error> for Error {227	fn from(e: anyhow::Error) -> Self {228		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))229	}230}231232impl From<ErrorKind> for Error {233	fn from(e: ErrorKind) -> Self {234		Self::new(e)235	}236}237238impl From<Infallible> for Error {239	fn from(_value: Infallible) -> Self {240		unreachable!()241	}242}243244/// Single stack trace frame245#[derive(Clone, Debug, Trace)]246pub struct StackTraceElement {247	/// Source of this frame248	/// Some frames only act as description, without attached source249	pub location: Option<Span>,250	/// Frame description251	pub desc: String,252}253#[derive(Debug, Clone, Trace)]254pub struct StackTrace(pub Vec<StackTraceElement>);255256#[derive(Clone, Trace)]257pub struct Error(Box<(ErrorKind, StackTrace)>);258impl Error {259	pub fn new(e: ErrorKind) -> Self {260		Self(Box::new((e, StackTrace(vec![]))))261	}262263	pub const fn error(&self) -> &ErrorKind {264		&(self.0).0265	}266	pub fn error_mut(&mut self) -> &mut ErrorKind {267		&mut (self.0).0268	}269	pub const fn trace(&self) -> &StackTrace {270		&(self.0).1271	}272	pub fn trace_mut(&mut self) -> &mut StackTrace {273		&mut (self.0).1274	}275}276impl fmt::Display for Error {277	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {278		writeln!(f, "{}", self.0 .0)?;279		for el in &self.0 .1 .0 {280			write!(f, "\t{}", el.desc)?;281			if let Some(loc) = &el.location {282				write!(f, "at {}", loc.0 .0 .0)?;283				loc.0.map_source_locations(&[loc.1, loc.2]);284			}285			writeln!(f)?;286		}287		Ok(())288	}289}290impl fmt::Debug for Error {291	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {292		f.debug_tuple("LocError").field(&self.0).finish()293	}294}295impl std::error::Error for Error {}296297pub trait ErrorSource {298	fn to_location(self) -> Option<Span>;299}300impl<T: Acyclic> ErrorSource for &Spanned<T> {301	fn to_location(self) -> Option<Span> {302		Some(self.span.clone())303	}304}305impl ErrorSource for &Span {306	fn to_location(self) -> Option<Span> {307		Some(self.clone())308	}309}310impl ErrorSource for CallLocation<'_> {311	fn to_location(self) -> Option<Span> {312		self.0.cloned()313	}314}315316pub type Result<V, E = Error> = std::result::Result<V, E>;317pub trait ResultExt: Sized {318	#[must_use]319	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;320	#[must_use]321	fn description(self, msg: &str) -> Self {322		self.with_description(|| msg)323	}324325	#[must_use]326	fn with_description_src<O: Into<String>>(327		self,328		src: impl ErrorSource,329		msg: impl FnOnce() -> O,330	) -> Self;331	#[must_use]332	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {333		self.with_description_src(src, || msg)334	}335}336impl<T> ResultExt for Result<T, Error> {337	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {338		if let Err(e) = &mut self {339			let trace = e.trace_mut();340			trace.0.push(StackTraceElement {341				location: None,342				desc: msg().into(),343			});344		}345		self346	}347348	fn with_description_src<O: Into<String>>(349		mut self,350		src: impl ErrorSource,351		msg: impl FnOnce() -> O,352	) -> Self {353		if let Err(e) = &mut self {354			let trace = e.trace_mut();355			trace.0.push(StackTraceElement {356				location: src.to_location(),357				desc: msg().into(),358			});359		}360		self361	}362}363364#[macro_export]365macro_rules! bail {366	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {367		return Err($w$(::$i)*$(($($tt)*))?.into())368	};369	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {370		return Err($w$(::$i)*$({$($tt)*})?.into())371	};372	($l:literal$(, $($tt:tt)*)?) => {373		return Err($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)).into())374	};375}376377#[macro_export]378macro_rules! runtime_error {379	($l:literal$(, $($tt:tt)*)?) => {380		$crate::error::Error::from($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)))381	};382}