git.delta.rocks / jrsonnet / refs/commits / 0afd46183b60

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	function::{CallLocation, FunctionSignature, ParamName},11	stdlib::format::FormatError,12	typed::TypeLocError,13	val::ConvertNumValueError,14	ObjValue, ResolvePathOwned,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!(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!");8283		heap.push((conf, field));84	}85	heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));86	heap.into_iter().map(|v| v.1).collect()87}8889/// Possible errors90#[allow(missing_docs)]91#[derive(Error, Debug, Clone, Trace)]92#[non_exhaustive]93pub enum ErrorKind {94	#[error("intrinsic not found: {0}")]95	IntrinsicNotFound(IStr),9697	#[error("operator {0} does not operate on type {1}")]98	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),99	#[error("binary operation {1} {0} {2} is not implemented")]100	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),101102	#[error("self/super/$ are only usable inside objects")]103	CantUseSelfSupOutsideOfObject,104	#[error("no super found")]105	NoSuperFound,106107	#[error("for loop can only iterate over arrays")]108	InComprehensionCanOnlyIterateOverArray,109110	#[error("array out of bounds: {0} is not within [0,{1})")]111	ArrayBoundsError(isize, usize),112	#[error("string out of bounds: {0} is not within [0,{1})")]113	StringBoundsError(usize, usize),114115	#[error("assert failed: {}", format_empty_str(.0))]116	AssertionFailed(IStr),117118	#[error("local is not defined: {0}{found}", found = format_found(.1, "local"))]119	VariableIsNotDefined(IStr, Vec<IStr>),120	#[error("duplicate local var: {0}")]121	DuplicateLocalVar(IStr),122123	#[error("type mismatch: expected {expected}, got {2} {0}", expected = .1.iter().map(|e| format!("{e}")).collect::<Vec<_>>().join(", "))]124	TypeMismatch(&'static str, Vec<ValType>, ValType),125	#[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]126	NoSuchField(IStr, Vec<IStr>),127128	#[error("only functions can be called, got {0}")]129	OnlyFunctionsCanBeCalledGot(ValType),130	#[error("parameter {0} is not defined")]131	UnknownFunctionParameter(IStr),132	#[error("argument {0} is already bound")]133	BindingParameterASecondTime(IStr),134	#[error("too many args, function has {0}\nFunction has the following signature: {1}")]135	TooManyArgsFunctionHas(usize, FunctionSignature),136	#[error("function argument is not passed: {0}\nFunction has the following signature: {1}")]137	FunctionParameterNotBoundInCall(ParamName, FunctionSignature),138139	#[error("external variable is not defined: {0}")]140	UndefinedExternalVariable(IStr),141142	#[error("field name should be string, got {0}")]143	FieldMustBeStringGot(ValType),144	#[error("duplicate field name: {}", format_empty_str(.0))]145	DuplicateFieldName(IStr),146147	#[error("attempted to index array with string {}", format_empty_str(.0))]148	AttemptedIndexAnArrayWithString(IStr),149	#[error("{0} index type should be {1}, got {2}")]150	ValueIndexMustBeTypeGot(ValType, ValType, ValType),151	#[error("cant index into {0}")]152	CantIndexInto(ValType),153	#[error("{0} is not indexable")]154	ValueIsNotIndexable(ValType),155156	#[error("super can't be used standalone")]157	StandaloneSuper,158159	#[error("can't resolve {1} from {0}")]160	ImportFileNotFound(SourcePath, ResolvePathOwned),161	#[error("resolved file not found: {:?}", .0)]162	ResolvedFileNotFound(SourcePath),163	#[error("can't import {0}: is a directory")]164	ImportIsADirectory(SourcePath),165	#[error("imported file is not valid utf-8: {0:?}")]166	ImportBadFileUtf8(SourcePath),167	#[error("import io error: {0}")]168	ImportIo(String),169	#[error("tried to import {1} from {0}, but imports are not supported")]170	ImportNotSupported(SourcePath, ResolvePathOwned),171	#[error("can't import from virtual file")]172	CantImportFromVirtualFile,173	#[error("syntax error: {error}")]174	ImportSyntaxError {175		path: Source,176		#[trace(skip)]177		error: Box<SyntaxError>,178	},179180	#[error("runtime error: {}", format_empty_str(.0))]181	RuntimeError(IStr),182	#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]183	StackOverflow,184	#[error("infinite recursion detected")]185	InfiniteRecursionDetected,186	#[error("tried to index by fractional value")]187	FractionalIndex,188	#[error("attempted to divide by zero")]189	DivisionByZero,190191	#[error("string manifest output is not an string")]192	StringManifestOutputIsNotAString,193	#[error("stream manifest output is not an array")]194	StreamManifestOutputIsNotAArray,195	#[error("multi manifest output is not an object")]196	MultiManifestOutputIsNotAObject,197198	#[error("cant recurse stream manifest")]199	StreamManifestOutputCannotBeRecursed,200	#[error("stream manifest output cannot consist of raw strings")]201	StreamManifestCannotNestString,202203	#[error("{}", format_empty_str(.0))]204	ImportCallbackError(String),205	#[error("invalid unicode codepoint: {0}")]206	InvalidUnicodeCodepointGot(u32),207208	#[error("convert num value: {0}")]209	ConvertNumValue(#[from] ConvertNumValueError),210211	#[error("format error: {0}")]212	Format(#[from] FormatError),213	#[error("type error: {0}")]214	TypeError(TypeLocError),215216	#[cfg(feature = "anyhow-error")]217	#[error(transparent)]218	Other(#[trace(skip)] std::rc::Rc<anyhow::Error>),219}220221#[cfg(feature = "anyhow-error")]222impl From<anyhow::Error> for Error {223	fn from(e: anyhow::Error) -> Self {224		Self::new(ErrorKind::Other(std::rc::Rc::new(e)))225	}226}227228impl From<ErrorKind> for Error {229	fn from(e: ErrorKind) -> Self {230		Self::new(e)231	}232}233234impl From<Infallible> for Error {235	fn from(_value: Infallible) -> Self {236		unreachable!()237	}238}239240/// Single stack trace frame241#[derive(Clone, Debug, Trace)]242pub struct StackTraceElement {243	/// Source of this frame244	/// Some frames only act as description, without attached source245	pub location: Option<Span>,246	/// Frame description247	pub desc: String,248}249#[derive(Debug, Clone, Trace)]250pub struct StackTrace(pub Vec<StackTraceElement>);251252#[derive(Clone, Trace)]253pub struct Error(Box<(ErrorKind, StackTrace)>);254impl Error {255	pub fn new(e: ErrorKind) -> Self {256		Self(Box::new((e, StackTrace(vec![]))))257	}258259	pub const fn error(&self) -> &ErrorKind {260		&(self.0).0261	}262	pub fn error_mut(&mut self) -> &mut ErrorKind {263		&mut (self.0).0264	}265	pub const fn trace(&self) -> &StackTrace {266		&(self.0).1267	}268	pub fn trace_mut(&mut self) -> &mut StackTrace {269		&mut (self.0).1270	}271}272impl fmt::Display for Error {273	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {274		writeln!(f, "{}", self.0 .0)?;275		for el in &self.0 .1 .0 {276			write!(f, "\t{}", el.desc)?;277			if let Some(loc) = &el.location {278				write!(f, "at {}", loc.0 .0 .0)?;279				loc.0.map_source_locations(&[loc.1, loc.2]);280			}281			writeln!(f)?;282		}283		Ok(())284	}285}286impl fmt::Debug for Error {287	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {288		f.debug_tuple("LocError").field(&self.0).finish()289	}290}291impl std::error::Error for Error {}292293pub trait ErrorSource {294	fn to_location(self) -> Option<Span>;295}296impl<T: Acyclic> ErrorSource for &Spanned<T> {297	fn to_location(self) -> Option<Span> {298		Some(self.span.clone())299	}300}301impl ErrorSource for &Span {302	fn to_location(self) -> Option<Span> {303		Some(self.clone())304	}305}306impl ErrorSource for CallLocation<'_> {307	fn to_location(self) -> Option<Span> {308		self.0.cloned()309	}310}311312pub type Result<V, E = Error> = std::result::Result<V, E>;313pub trait ResultExt: Sized {314	#[must_use]315	fn with_description<O: Into<String>>(self, msg: impl FnOnce() -> O) -> Self;316	#[must_use]317	fn description(self, msg: &str) -> Self {318		self.with_description(|| msg)319	}320321	#[must_use]322	fn with_description_src<O: Into<String>>(323		self,324		src: impl ErrorSource,325		msg: impl FnOnce() -> O,326	) -> Self;327	#[must_use]328	fn description_src(self, src: impl ErrorSource, msg: &str) -> Self {329		self.with_description_src(src, || msg)330	}331}332impl<T> ResultExt for Result<T, Error> {333	fn with_description<O: Into<String>>(mut self, msg: impl FnOnce() -> O) -> Self {334		if let Err(e) = &mut self {335			let trace = e.trace_mut();336			trace.0.push(StackTraceElement {337				location: None,338				desc: msg().into(),339			});340		}341		self342	}343344	fn with_description_src<O: Into<String>>(345		mut self,346		src: impl ErrorSource,347		msg: impl FnOnce() -> O,348	) -> Self {349		if let Err(e) = &mut self {350			let trace = e.trace_mut();351			trace.0.push(StackTraceElement {352				location: src.to_location(),353				desc: msg().into(),354			});355		}356		self357	}358}359360#[macro_export]361macro_rules! bail {362	($w:ident$(::$i:ident)*$(($($tt:tt)*))?) => {363		return Err($w$(::$i)*$(($($tt)*))?.into())364	};365	($w:ident$(::$i:ident)*$({$($tt:tt)*})?) => {366		return Err($w$(::$i)*$({$($tt)*})?.into())367	};368	($l:literal$(, $($tt:tt)*)?) => {369		return Err($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)).into())370	};371}372373#[macro_export]374macro_rules! runtime_error {375	($l:literal$(, $($tt:tt)*)?) => {376		$crate::error::Error::from($crate::error::ErrorKind::RuntimeError($crate::jrsonnet_macros::format_istr!($l$(, $($tt)*)?)))377	};378}