From 67addf3b3d5533f0b8f37d5d981c31e4313cf317 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 16 Jan 2023 13:05:45 +0300 Subject: feat(tvix/eval): add error variant for threading through errors This variant is required for external builtins (which in our case includes `derivation`) to thread through reasonable error messages. This has some potential for improvement, but it's an improvement over the status quo of panicking in the external builtins when no appropriate error is available. Change-Id: I7e4bdb0a156c7717092dde30aa4785192182dc66 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7841 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/eval/src/errors.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 67ef509ba9d7..ed161f4155f5 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -1,5 +1,3 @@ -use crate::spans::ToSpan; -use crate::value::{CoercionKind, NixString}; use std::error; use std::io; use std::path::PathBuf; @@ -14,6 +12,8 @@ use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, Spa use smol_str::SmolStr; use xml::writer::Error as XmlError; +use crate::spans::ToSpan; +use crate::value::{CoercionKind, NixString}; use crate::{SourceCode, Value}; #[derive(Clone, Debug)] @@ -143,6 +143,10 @@ pub enum ErrorKind { /// Errors while serialising to XML. Xml(Rc), + /// Variant for errors that bubble up to eval from other Tvix + /// components. + TvixError(Rc), + /// Variant for code paths that are known bugs in Tvix (usually /// issues with the compiler/VM interaction). TvixBug { @@ -170,6 +174,7 @@ impl error::Error for Error { } ErrorKind::IO { error, .. } => Some(error.as_ref()), ErrorKind::Xml(error) => Some(error.as_ref()), + ErrorKind::TvixError(error) => Some(error.as_ref()), _ => None, } } @@ -412,6 +417,10 @@ to a missing value in the attribute set(s) included via `with`."#, ErrorKind::Xml(error) => write!(f, "failed to serialise to XML: {error}"), + ErrorKind::TvixError(inner_error) => { + write!(f, "{inner_error}") + } + ErrorKind::TvixBug { msg, metadata } => { write!(f, "Tvix bug: {}", msg)?; @@ -710,6 +719,7 @@ impl Error { | ErrorKind::IO { .. } | ErrorKind::FromJsonError(_) | ErrorKind::Xml(_) + | ErrorKind::TvixError(_) | ErrorKind::TvixBug { .. } | ErrorKind::NotImplemented(_) => return None, }; @@ -754,6 +764,11 @@ impl Error { ErrorKind::DivisionByZero => "E033", ErrorKind::Xml(_) => "E034", + // Special error code for errors from other Tvix + // components. We may want to introduce a code namespacing + // system to have these errors pass codes through. + ErrorKind::TvixError(_) => "E997", + // Special error code that is not part of the normal // ordering. ErrorKind::TvixBug { .. } => "E998", -- cgit 1.4.1