about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-01-26T22·42+0100
committerclbot <clbot@tvl.fyi>2023-01-27T14·08+0000
commite3c2b3650aff8fbd4a5109cdb3406d92e9806d08 (patch)
tree060637af042dcfbdc85c02067458b85145e49e40
parenta94a1434cc2a57b330a2ad6f310573fb70e15e8a (diff)
refactor(tvix/cli): describe errors with thiserror r/5773
This is much less code, and makes it much easier to read.

Change-Id: I9028f226105f905c2cc2cabd33907ff493e26225
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7938
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
-rw-r--r--tvix/Cargo.lock1
-rw-r--r--tvix/Cargo.nix4
-rw-r--r--tvix/cli/Cargo.toml1
-rw-r--r--tvix/cli/src/errors.rs79
4 files changed, 21 insertions, 64 deletions
diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock
index 1eb25afbe8ed..b7d478a779b7 100644
--- a/tvix/Cargo.lock
+++ b/tvix/Cargo.lock
@@ -2615,6 +2615,7 @@ dependencies = [
  "smol_str",
  "ssri",
  "test-case",
+ "thiserror",
  "tvix-derivation",
  "tvix-eval",
  "tvix-store-bin",
diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix
index 3160d02132ca..8255a395fcbf 100644
--- a/tvix/Cargo.nix
+++ b/tvix/Cargo.nix
@@ -7734,6 +7734,10 @@ rec {
             packageId = "ssri";
           }
           {
+            name = "thiserror";
+            packageId = "thiserror";
+          }
+          {
             name = "tvix-derivation";
             packageId = "tvix-derivation";
           }
diff --git a/tvix/cli/Cargo.toml b/tvix/cli/Cargo.toml
index 83796855b672..56d95f7460f7 100644
--- a/tvix/cli/Cargo.toml
+++ b/tvix/cli/Cargo.toml
@@ -18,6 +18,7 @@ smol_str = "0.1"
 aho-corasick = "0.7"
 ssri = "7.0.0"
 data-encoding = "2.3.3"
+thiserror = "1.0.38"
 
 [dev-dependencies]
 test-case = "2.2.2"
diff --git a/tvix/cli/src/errors.rs b/tvix/cli/src/errors.rs
index 5791c5332bfc..cc402c9a9d45 100644
--- a/tvix/cli/src/errors.rs
+++ b/tvix/cli/src/errors.rs
@@ -1,83 +1,34 @@
-use std::{error, fmt::Display, rc::Rc};
+use std::rc::Rc;
+use thiserror::Error;
 use tvix_derivation::DerivationError;
 
-#[derive(Debug, PartialEq)]
+/// Errors related to derivation construction
+#[derive(Debug, Error, PartialEq)]
 pub enum Error {
-    // Errors related to derivation construction
+    #[error("an output with the name '{0}' is already defined")]
     DuplicateOutput(String),
+    #[error("fixed-output derivations can only have the default `out`-output")]
     ConflictingOutputTypes,
+    #[error("the environment variable '{0}' has already been set in this derivation")]
     DuplicateEnvVar(String),
+    #[error("the environment variable '{0}' shadows the name of an output")]
     ShadowedOutput(String),
+    #[error("invalid derivation parameters: {0}")]
     InvalidDerivation(DerivationError),
+    #[error("invalid output hash mode: '{0}', only 'recursive' and 'flat` are supported")]
     InvalidOutputHashMode(String),
+    #[error("unsupported sri algorithm: {0}, only sha1, sha256 or sha512 is supported")]
     UnsupportedSRIAlgo(String),
+    #[error("invalid number of sri hashes in string ({0}), only one hash is supported")]
     UnsupportedSRIMultiple(usize),
+    #[error("invalid sri digest: {0}")]
     InvalidSRIDigest(data_encoding::DecodeError),
+    #[error("failed to parse SRI string: {0}")]
     InvalidSRIString(String),
+    #[error("outputHashAlgo is set to {0}, but outputHash contains SRI with algo {1}")]
     ConflictingSRIHashAlgo(String, String),
 }
 
-impl Display for Error {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            Error::DuplicateOutput(name) => {
-                write!(f, "an output with the name '{name}' is already defined")
-            }
-
-            Error::ConflictingOutputTypes => write!(
-                f,
-                "fixed-output derivations can only have the default `out`-output"
-            ),
-
-            Error::DuplicateEnvVar(name) => write!(
-                f,
-                "the environment variable '{name}' has already been set in this derivation"
-            ),
-            Error::ShadowedOutput(name) => write!(
-                f,
-                "the environment variable '{name}' shadows the name of an output"
-            ),
-            Error::InvalidDerivation(error) => write!(f, "invalid derivation parameters: {error}"),
-
-            Error::InvalidOutputHashMode(mode) => write!(
-                f,
-                "invalid output hash mode: '{mode}', only 'recursive' and 'flat` are supported"
-            ),
-            Error::UnsupportedSRIAlgo(algo) => {
-                write!(
-                    f,
-                    "unsupported sri algorithm: {algo}, only sha1, sha256 or sha512 is supported"
-                )
-            }
-            Error::UnsupportedSRIMultiple(n) => {
-                write!(
-                    f,
-                    "invalid number of sri hashes in string ({n}), only one hash is supported"
-                )
-            }
-            Error::InvalidSRIDigest(err) => {
-                write!(f, "invalid sri digest: {}", err)
-            }
-            Error::InvalidSRIString(err) => {
-                write!(f, "failed to parse SRI string: {}", err)
-            }
-            Error::ConflictingSRIHashAlgo(algo, sri_algo) => {
-                write!(
-                    f,
-                    "outputHashAlgo is set to {}, but outputHash contains SRI with algo {}",
-                    algo, sri_algo
-                )
-            }
-        }
-    }
-}
-
-impl error::Error for Error {
-    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
-        None
-    }
-}
-
 impl From<Error> for tvix_eval::ErrorKind {
     fn from(err: Error) -> Self {
         tvix_eval::ErrorKind::TvixError(Rc::new(err))