git.delta.rocks / jrsonnet / refs/commits / 75beb619a12f

difftreelog

source

crates/jrsonnet-evaluator/src/error.rs10.1 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	ObjValue, ResolvePathOwned,11	function::{CallLocation, FunctionSignature, ParamName},12	stdlib::format::FormatError,13	typed::TypeLocError,14	val::ConvertNumValueError,15};1617#[derive(Debug, Clone)]18pub struct SyntaxErrorLocation {19	pub offset: usize,20}2122#[derive(Debug, Clone)]23pub struct SyntaxError {24	pub message: String,25	pub location: SyntaxErrorLocation,26}27impl fmt::Display for SyntaxError {28	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {29		write!(f, "{}", self.message)30	}31}3233pub(crate) fn format_found(list: &[IStr], what: &str) -> String {34	if list.is_empty() {35		return String::new();36	}37	let mut out = String::new();38	out.push_str("\nThere ");39	if list.len() > 1 {40		out.push_str("are ");41	} else {42		out.push_str("is a ");43	}44	out.push_str(what);45	if list.len() > 1 {46		out.push('s');47	}48	out.push_str(" with similar name");49	if list.len() > 1 {50		out.push('s');51	}52	out.push_str(" present: ");53	for (i, v) in list.iter().enumerate() {54		if i != 0 {55			out.push_str(", ");56		}57		out.push_str(v as &str);58	}59	out60}6162const fn format_empty_str(str: &str) -> &str {63	if str.is_empty() {64		"\"\" (empty string)"65	} else {66		str67	}68}6970pub(crate) fn suggest_object_fields(v: &ObjValue, key: IStr) -> Vec<IStr> {71	let mut heap = Vec::new();72	for field in v.fields_ex(73		true,74		#[cfg(feature = "exp-preserve-order")]75		false,76	) {77		let conf = strsim::jaro_winkler(field.as_str(), key.as_str());78		if conf < 0.8 {79			continue;80		}81		assert!(82			field.as_str() != key.as_str(),83			"looks like string pooling failure, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!"84		);8586		heap.push((conf, field));87	}88	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));89	heap.into_iter().map(|v| v.1).collect()90}9192/// Possible errors93#[allow(missing_docs)]94#[derive(Error, Debug, Clone, Trace)]95#[non_exhaustive]96pub enum ErrorKind {97	#[error("intrinsic not found: {0}")]98	IntrinsicNotFound(IStr),99100	#[error("operator {0} does not operate on type {1}")]101	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),102	#[error("binary operation {1} {0} {2} is not implemented")]103	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),104105	#[error("self/super/$ are only usable inside objects")]106	CantUseSelfSupOutsideOfObject,107	#[error("no super found")]108	NoSuperFound,109110	#[error("for loop can only iterate over arrays")]111	InComprehensionCanOnlyIterateOverArray,112113	#[error("array out of bounds: {0} is not within [0,{1})")]114	ArrayBoundsError(isize, usize),115	#[error("string out of bounds: {0} is not within [0,{1})")]116	StringBoundsError(usize, usize),117118	#[error("assert failed: {}", format_empty_str(.0))]119	AssertionFailed(IStr),120121	#[error("local is not defined: {0}{found}", found = format_found(.1, "local"))]122	VariableIsNotDefined(IStr, Vec<IStr>),123	#[error("duplicate local var: {0}")]124	DuplicateLocalVar(IStr),125126	#[error("type mismatch: expected {expected}, got {2} {0}", expected = .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]127	TypeMismatch(&'static str, Vec<ValType>, ValType),128	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]129	NoSuchField(IStr, Vec<IStr>),130131	#[error("only functions can be called, got {0}")]132	OnlyFunctionsCanBeCalledGot(ValType),133	#[error("parameter {0} is not defined")]134	UnknownFunctionParameter(IStr),135	#[error("argument {0} is already bound")]136	BindingParameterASecondTime(IStr),137	#[error("too many args, function has {0}\nFunction has the following signature: {1}")]138	TooManyArgsFunctionHas(usize, FunctionSignature),139	#[error("function argument is not passed: {0}\nFunction has the following signature: {1}")]140	FunctionParameterNotBoundInCall(ParamName, FunctionSignature),141142	#[error("external variable is not defined: {0}")]143	UndefinedExternalVariable(IStr),144145	#[error("field name should be string, got {0}")]146	FieldMustBeStringGot(ValType),147	#[error("duplicate field name: {}", format_empty_str(.0))]148	DuplicateFieldName(IStr),149150	#[error("attempted to index array with string {}", format_empty_str(.0))]151	AttemptedIndexAnArrayWithString(IStr),152	#[error("{0} index type should be {1}, got {2}")]153	ValueIndexMustBeTypeGot(ValType, ValType, ValType),154	#[error("cant index into {0}")]155	CantIndexInto(ValType),156	#[error("{0} is not indexable")]157	ValueIsNotIndexable(ValType),158159	#[error("super can't be used standalone")]160	StandaloneSuper,161162	#[error("can't resolve {1} from {0}")]163	ImportFileNotFound(SourcePath, ResolvePathOwned),164	#[error("resolved file not found: {:?}", .0)]165	ResolvedFileNotFound(SourcePath),166	#[error("can't import {0}: is a directory")]167	ImportIsADirectory(SourcePath),168	#[error("imported file is not valid utf-8: {0:?}")]169	ImportBadFileUtf8(SourcePath),170	#[error("import io error: {0}")]171	ImportIo(String),172	#[error("tried to import {1} from {0}, but imports are not supported")]173	ImportNotSupported(SourcePath, ResolvePathOwned),174	#[error("can't import from virtual file")]175	CantImportFromVirtualFile,176	#[error("syntax error: {error}")]177	ImportSyntaxError {178		path: Source,179		#[trace(skip)]180		error: Box<SyntaxError>,181	},182183	#[error("runtime error: {}", format_empty_str(.0))]184	RuntimeError(IStr),185	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]186	StackOverflow,187	#[error("infinite recursion detected")]188	InfiniteRecursionDetected,189	#[error("tried to index by fractional value")]190	FractionalIndex,191	#[error("attempted to divide by zero")]192	DivisionByZero,193194	#[error("string manifest output is not an string")]195	StringManifestOutputIsNotAString,196	#[error("stream manifest output is not an array")]197	StreamManifestOutputIsNotAArray,198	#[error("multi manifest output is not an object")]199	MultiManifestOutputIsNotAObject,200201	#[error("cant recurse stream manifest")]202	StreamManifestOutputCannotBeRecursed,203	#[error("stream manifest output cannot consist of raw strings")]204	StreamManifestCannotNestString,205206	#[error("{}", format_empty_str(.0))]207	ImportCallbackError(String),208	#[error("invalid unicode codepoint: {0}")]209	InvalidUnicodeCodepointGot(u32),210211	#[error("convert num value: {0}")]212	ConvertNumValue(#[from] ConvertNumValueError),213214	#[error("format error: {0}")]215	Format(#[from] FormatError),216	#[error("type error: {0}")]217	TypeError(TypeLocError),218219	#[cfg(feature = "anyhow-error")]220	#[error(transparent)]221	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),222}223224#[cfg(feature = "anyhow-error")]225impl From<anyhow::Error> for Error {226	fn from(e: anyhow::Error) -> Self {227		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))228	}229}230231impl From<ErrorKind> for Error {232	fn from(e: ErrorKind) -> Self {233		Self::new(e)234	}235}236237impl From<Infallible> for Error {238	fn from(_value: Infallible) -> Self {239		unreachable!()240	}241}242243/// Single stack trace frame244#[derive(Clone, Debug, Trace)]245pub struct StackTraceElement {246	/// Source of this frame247	/// Some frames only act as description, without attached source248	pub location: Option<Span>,249	/// Frame description250	pub desc: String,251}252#[derive(Debug, Clone, Trace)]253pub struct StackTrace(pub Vec<StackTraceElement>);254255#[derive(Clone, Trace)]256pub struct Error(Box<(ErrorKind, StackTrace)>);257impl Error {258	pub fn new(e: ErrorKind) -> Self {259		Self(Box::new((e, StackTrace(vec![]))))260	}261262	pub const fn error(&self) -> &ErrorKind {263		&(self.0).0264	}265	pub fn error_mut(&mut self) -> &mut ErrorKind {266		&mut (self.0).0267	}268	pub const fn trace(&self) -> &StackTrace {269		&(self.0).1270	}271	pub fn trace_mut(&mut self) -> &mut StackTrace {272		&mut (self.0).1273	}274}275impl fmt::Display for Error {276	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {277		writeln!(f, "{}", self.0.0)?;278		for el in &self.0.1.0 {279			write!(f, "\t{}", el.desc)?;280			if let Some(loc) = &el.location {281				write!(f, "at {}", loc.0.0.0)?;282				loc.0.map_source_locations(&[loc.1, loc.2]);283			}284			writeln!(f)?;285		}286		Ok(())287	}288}289impl fmt::Debug for Error {290	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {291		f.debug_tuple("LocError").field(&self.0).finish()292	}293}294impl std::error::Error for Error {}295296pub trait ErrorSource {297	fn to_location(self) -> Option<Span>;298}299impl<T: Acyclic> ErrorSource for &Spanned<T> {300	fn to_location(self) -> Option<Span> {301		Some(self.span.clone())302	}303}304impl ErrorSource for &Span {305	fn to_location(self) -> Option<Span> {306		Some(self.clone())307	}308}309impl ErrorSource for CallLocation<'_> {310	fn to_location(self) -> Option<Span> {311		self.0.cloned()312	}313}314315pub type Result<V, E = Error> = std::result::Result<V, E>;316pub trait ResultExt: Sized {317	#[must_use]318	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;319	#[must_use]320	fn description(self, msg: &str) -> Self {321		self.with_description(|| msg)322	}323324	#[must_use]325	fn with_description_src<O: Into<String>>(326		self,327		src: impl ErrorSource,328		msg: impl FnOnce() -> O,329	) -> Self;330	#[must_use]331	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {332		self.with_description_src(src, || msg)333	}334}335impl<T> ResultExt for Result<T, Error> {336	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {337		if let Err(e) = &mut self {338			let trace = e.trace_mut();339			trace.0.push(StackTraceElement {340				location: None,341				desc: msg().into(),342			});343		}344		self345	}346347	fn with_description_src<O: Into<String>>(348		mut self,349		src: impl ErrorSource,350		msg: impl FnOnce() -> O,351	) -> Self {352		if let Err(e) = &mut self {353			let trace = e.trace_mut();354			trace.0.push(StackTraceElement {355				location: src.to_location(),356				desc: msg().into(),357			});358		}359		self360	}361}362363#[macro_export]364macro_rules! bail {365	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {366		return Err($w$(::$i)*$(($($tt)*))?.into())367	};368	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {369		return Err($w$(::$i)*$({$($tt)*})?.into())370	};371	($l:literal$(, $($tt:tt)*)?) => {372		return Err($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)).into())373	};374}375376#[macro_export]377macro_rules! runtime_error {378	($l:literal$(, $($tt:tt)*)?) => {379		$crate::error::Error::from($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)))380	};381}