snix/tvix/glue/src/builtins/errors.rs
Aspen Smith 72b9a126b8 feat(tvix/glue): Implement builtins.storePath
This one's relatively simple - we just check if the store path exists,
and if it does we make a new contextful string containing the store path
as its only context element.

Automatic testing seems tricky for this (I think?) so I tested it
manually:

tvix-repl> builtins.storePath /nix/store/yn46i4xx5alh7gs6fpkxk430i34rp2q9-hello-2.12.1
=> "/nix/store/yn46i4xx5alh7gs6fpkxk430i34rp2q9-hello-2.12.1" :: string

Change-Id: I8a0d9726e4102ab872c53c2419679c2c855a5a18
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11696
Tested-by: BuildkiteCI
Autosubmit: aspen <root@gws.fyi>
Reviewed-by: flokli <flokli@flokli.de>
2024-06-05 17:50:15 +00:00

80 lines
2.5 KiB
Rust

//! Contains errors that can occur during evaluation of builtins in this crate
use nix_compat::{
nixhash::{self, NixHash},
store_path::BuildStorePathError,
};
use reqwest::Url;
use std::{path::PathBuf, rc::Rc};
use thiserror::Error;
use tvix_castore::import;
/// Errors related to derivation construction
#[derive(Debug, Error)]
pub enum DerivationError {
#[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("invalid derivation parameters: {0}")]
InvalidDerivation(#[from] nix_compat::derivation::DerivationError),
#[error("invalid output hash: {0}")]
InvalidOutputHash(#[from] nixhash::Error),
#[error("invalid output hash mode: '{0}', only 'recursive' and 'flat` are supported")]
InvalidOutputHashMode(String),
}
impl From<DerivationError> for tvix_eval::ErrorKind {
fn from(err: DerivationError) -> Self {
tvix_eval::ErrorKind::TvixError(Rc::new(err))
}
}
#[derive(Debug, Error)]
pub enum FetcherError {
#[error("hash mismatch in file downloaded from {url}:\n wanted: {wanted}\n got: {got}")]
HashMismatch {
url: Url,
wanted: NixHash,
got: NixHash,
},
#[error("Invalid hash type '{0}' for fetcher")]
InvalidHashType(&'static str),
#[error("Unable to parse URL: {0}")]
InvalidUrl(#[from] url::ParseError),
#[error(transparent)]
Http(#[from] reqwest::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Import(#[from] tvix_castore::import::IngestionError<import::archive::Error>),
#[error("Error calculating store path for fetcher output: {0}")]
StorePath(#[from] BuildStorePathError),
}
/// Errors related to `builtins.path` and `builtins.filterSource`,
/// a.k.a. "importing" builtins.
#[derive(Debug, Error)]
pub enum ImportError {
#[error("non-file '{0}' cannot be imported in 'flat' mode")]
FlatImportOfNonFile(String),
#[error("hash mismatch at ingestion of '{0}', expected: '{1}', got: '{2}'")]
HashMismatch(String, NixHash, NixHash),
#[error("path '{}' is not in the Nix store", .0.display())]
PathNotInStore(PathBuf),
}
impl From<ImportError> for tvix_eval::ErrorKind {
fn from(err: ImportError) -> Self {
tvix_eval::ErrorKind::TvixError(Rc::new(err))
}
}