about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-01-16T10·05+0300
committertazjin <tazjin@tvl.su>2023-01-16T13·43+0000
commit67addf3b3d5533f0b8f37d5d981c31e4313cf317 (patch)
treebdb0159669a0b2085931a9b37caaab67cb736a73
parentd365b092262013e074f0fe800a1955032eaa2fd9 (diff)
feat(tvix/eval): add error variant for threading through errors r/5665
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 <flokli@flokli.de>
Tested-by: BuildkiteCI
-rw-r--r--tvix/eval/src/errors.rs19
1 files 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<XmlError>),
 
+    /// Variant for errors that bubble up to eval from other Tvix
+    /// components.
+    TvixError(Rc<dyn error::Error>),
+
     /// 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",