feat(tvix/nix-compat): fix and improve error message

Similar to cl/9350. Most of the times, this is still a regular string,
so let's print it like that if we can, and resort to base64 encoding if
we can't.

We were also wrongly always outputting the second character in the
string.

Change-Id: Id0e2a9d9f1ad3d2d7b554893ecd89a7e6383e9c2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9445
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
Florian Klink 2023-09-23 11:48:20 +03:00 committed by flokli
parent 8de0d6ad48
commit 09a901de5b

View file

@ -1,4 +1,5 @@
use crate::nixbase32::{self, Nixbase32DecodeError};
use data_encoding::BASE64;
use std::{fmt, path::PathBuf, str::FromStr};
use thiserror;
@ -28,7 +29,11 @@ pub enum Error {
InvalidHashEncoding(Nixbase32DecodeError),
#[error("Invalid length")]
InvalidLength(),
#[error("Invalid name: {:?}, character at position {} ('{}') is invalid", .0, .1, .0[1])]
#[error(
"Invalid name: \"{}\", character at position {} is invalid",
std::str::from_utf8(&.0).unwrap_or(&BASE64.encode(.0)),
.1,
)]
InvalidName(Vec<u8>, usize),
#[error("Tried to parse an absolute path which was missing the store dir prefix.")]
MissingStoreDir(),