From 460865b26c3a06950ec381e729b9c410a66ab116 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Wed, 9 Oct 2024 08:36:45 +0200 Subject: [PATCH] fix(errors): use correct error variant for data encryption (#323) Based on #322 --------- Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- crates/core/src/crypto/aespoly1305.rs | 2 +- crates/core/src/error.rs | 197 +++++++++++++------------- 2 files changed, 101 insertions(+), 98 deletions(-) diff --git a/crates/core/src/crypto/aespoly1305.rs b/crates/core/src/crypto/aespoly1305.rs index d22e49c1..c068f9b1 100644 --- a/crates/core/src/crypto/aespoly1305.rs +++ b/crates/core/src/crypto/aespoly1305.rs @@ -111,7 +111,7 @@ impl CryptoKey for Key { res.extend_from_slice(data); let tag = Aes256CtrPoly1305Aes::new(&self.0) .encrypt_in_place_detached(&nonce, &[], &mut res[16..]) - .map_err(CryptoErrorKind::DataDecryptionFailed)?; + .map_err(CryptoErrorKind::DataEncryptionFailed)?; res.extend_from_slice(&tag); Ok(res) } diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 8c6c0967..844b14f1 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -175,69 +175,74 @@ pub enum RusticErrorKind { /// [`CommandErrorKind`] describes the errors that can happen while executing a high-level command #[derive(Error, Debug, Display)] pub enum CommandErrorKind { - /// path is no dir: `{0:?}` + /// path is no dir: `{0}` PathIsNoDir(String), - /// used blobs are missing: blob {0} doesn't existing + /// used blobs are missing: blob `{0}` doesn't existing BlobsMissing(BlobId), - /// used pack {0}: size does not match! Expected size: {1}, real size: {2} + /// used pack `{0}`: size does not match! Expected size: `{1}`, real size: `{2}` PackSizeNotMatching(PackId, u32, u32), - /// "used pack {0} does not exist! + /// used pack `{0}` does not exist! PackNotExisting(PackId), - /// pack {0} got no decision what to do + /// pack `{0}` got no decision what to do NoDecision(PackId), - /// {0:?} + /// [`std::num::ParseFloatError`] + #[error(transparent)] FromParseFloatError(#[from] ParseFloatError), - /// {0:?} + /// [`std::num::ParseIntError`] + #[error(transparent)] FromParseIntError(#[from] ParseIntError), - /// {0} + /// Bytesize parser failed: `{0}` FromByteSizeParser(String), /// --repack-uncompressed makes no sense for v1 repo! RepackUncompressedRepoV1, - /// datetime out of range: `{0:?}` + /// datetime out of range: `{0}` FromOutOfRangeError(#[from] OutOfRangeError), - /// node type {0:?} not supported by dump + /// node type `{0:?}` not supported by dump DumpNotSupported(NodeType), - /// {0:?} + /// [`serde_json::Error`] + #[error(transparent)] FromJsonError(#[from] serde_json::Error), - /// version {0} is not supported. Allowed values: {1:?} + /// version `{0}` is not supported. Allowed values: {1:?} VersionNotSupported(u32, RangeInclusive), - /// cannot downgrade version from {0} to {1} + /// cannot downgrade version from `{0}` to `{1}` CannotDowngrade(u32, u32), - /// compression level {0} is not supported for repo v1 + /// compression level `{0}` is not supported for repo v1 NoCompressionV1Repo(i32), - /// compression level {0} is not supported. Allowed values: {1:?} + /// compression level `{0}` is not supported. Allowed values: `{1:?}` CompressionLevelNotSupported(i32, RangeInclusive), - /// Size is too large: {0} + /// Size is too large: `{0}` SizeTooLarge(bytesize::ByteSize), /// min_packsize_tolerate_percent must be <= 100 MinPackSizeTolerateWrong, /// max_packsize_tolerate_percent must be >= 100 or 0" MaxPackSizeTolerateWrong, - /// error creating {0:?}: {1:?} + /// error creating `{0:?}`: `{1:?}` ErrorCreating(PathBuf, Box), - /// error collecting information for {0:?}: {1:?} + /// error collecting information for `{0:?}`: `{1:?}` ErrorCollecting(PathBuf, Box), - /// error setting length for {0:?}: {1:?} + /// error setting length for `{0:?}`: `{1:?}` ErrorSettingLength(PathBuf, Box), - /// {0:?} + /// [`rayon::ThreadPoolBuildError`] + #[error(transparent)] FromRayonError(#[from] rayon::ThreadPoolBuildError), - /// conversion from integer failed: `{0:?}` + /// Conversion from integer failed: `{0:?}` ConversionFromIntFailed(TryFromIntError), - /// {0} is not allowed on an append-only repository + /// Not allowed on an append-only repository: `{0}` NotAllowedWithAppendOnly(String), /// Specify one of the keep-* options for forget! Please use keep-none to keep no snapshot. NoKeepOption, - /// {0:?} + /// [`shell_words::ParseError`] + #[error(transparent)] FromParseError(#[from] shell_words::ParseError), } /// [`CryptoErrorKind`] describes the errors that can happen while dealing with Cryptographic functions #[derive(Error, Debug, Display, Copy, Clone)] pub enum CryptoErrorKind { - /// data decryption failed + /// data decryption failed: `{0:?}` DataDecryptionFailed(aead::Error), - /// data encryption failed - DataEncryptionFailed, + /// data encryption failed: `{0:?}` + DataEncryptionFailed(aead::Error), /// crypto key too short CryptoKeyTooShort, } @@ -276,11 +281,11 @@ pub enum RepositoryErrorKind { NoIDSpecified, /// error opening password file `{0:?}` OpeningPasswordFileFailed(std::io::Error), - /// No repository config file found. Is there a repo at {0}? + /// No repository config file found. Is there a repo at `{0}`? NoRepositoryConfigFound(String), - /// More than one repository config file at {0}. Aborting. + /// More than one repository config file at `{0}`. Aborting. MoreThanOneRepositoryConfig(String), - /// keys from repo and repo-hot do not match for {0}. Aborting. + /// keys from repo and repo-hot do not match for `{0}`. Aborting. KeysDontMatchForRepositories(String), /// repository is a hot repository!\nPlease use as --repo-hot in combination with the normal repo. Aborting. HotRepositoryFlagMissing, @@ -292,9 +297,9 @@ pub enum RepositoryErrorKind { PasswordCommandExecutionFailed, /// error reading password from command ReadingPasswordFromCommandFailed, - /// running command {0}:{1} was not successful: {2} + /// running command `{0}`:`{1}` was not successful: `{2}` CommandExecutionFailed(String, String, std::io::Error), - /// running command {0}:{1} returned status: {2} + /// running command {0}:{1} returned status: `{2}` CommandErrorStatus(String, String, ExitStatus), /// error listing the repo config file ListingRepositoryConfigFileFailed, @@ -304,7 +309,7 @@ pub enum RepositoryErrorKind { ListingHotRepositoryKeysFailed, /// error accessing config file AccessToConfigFileFailed, - /// {0:?} + /// Thread pool build error: `{0:?}` FromThreadPoolbilderError(rayon::ThreadPoolBuildError), /// reading Password failed: `{0:?}` ReadingPasswordFromReaderFailed(std::io::Error), @@ -312,7 +317,7 @@ pub enum RepositoryErrorKind { ReadingPasswordFromPromptFailed(std::io::Error), /// Config file already exists. Aborting. ConfigFileExists, - /// did not find id {0} in index + /// did not find id `{0}` in index IdNotFound(BlobId), /// no suitable backend type found NoBackendTypeGiven, @@ -332,33 +337,34 @@ pub enum IndexErrorKind { /// [`BackendAccessErrorKind`] describes the errors that can be returned by the various Backends #[derive(Error, Debug, Display)] pub enum BackendAccessErrorKind { - /// backend {0:?} is not supported! + /// backend `{0:?}` is not supported! BackendNotSupported(String), - /// backend {0} cannot be loaded: {1:?} + /// backend `{0}` cannot be loaded: {1:?} BackendLoadError(String, anyhow::Error), - /// no suitable id found for {0} + /// no suitable id found for `{0}` NoSuitableIdFound(String), - /// id {0} is not unique + /// id `{0}` is not unique IdNotUnique(String), - /// {0:?} + /// [`std::io::Error`] #[error(transparent)] FromIoError(#[from] std::io::Error), - /// {0:?} + /// [`std::num::TryFromIntError`] #[error(transparent)] FromTryIntError(#[from] TryFromIntError), - /// {0:?} + /// [`LocalDestinationErrorKind`] #[error(transparent)] FromLocalError(#[from] LocalDestinationErrorKind), - /// {0:?} + /// [`IdErrorKind`] #[error(transparent)] FromIdError(#[from] IdErrorKind), - /// {0:?} + /// [`IndexErrorKind`] #[error(transparent)] FromIgnoreError(#[from] IgnoreErrorKind), - /// {0:?} + /// [`CryptBackendErrorKind`] #[error(transparent)] FromBackendDecryptionError(#[from] CryptBackendErrorKind), - /// generic Ignore error: `{0:?}` + /// [`ignore::Error`] + #[error(transparent)] GenericError(#[from] ignore::Error), /// creating data in backend failed CreatingDataOnBackendFailed, @@ -400,22 +406,22 @@ pub enum KeyFileErrorKind { ConversionFromU32ToU8Failed(TryFromIntError), /// output length is invalid: `{0:?}` OutputLengthInvalid(scrypt::errors::InvalidOutputLen), - /// invalid scrypt parameters + /// invalid scrypt parameters: `{0:?}` InvalidSCryptParameters(scrypt::errors::InvalidParams), } /// [`PackFileErrorKind`] describes the errors that can be returned for `PackFile`s #[derive(Error, Debug, Display)] pub enum PackFileErrorKind { - /// Failed reading binary representation of the pack header + /// Failed reading binary representation of the pack header: `{0:?}` ReadingBinaryRepresentationFailed(binrw::Error), - /// Failed writing binary representation of the pack header + /// Failed writing binary representation of the pack header: `{0:?}` WritingBinaryRepresentationFailed(binrw::Error), - /// Read header length is too large! Length: {size_real}, file size: {pack_size} + /// Read header length is too large! Length: `{size_real}`, file size: `{pack_size}` HeaderLengthTooLarge { size_real: u32, pack_size: u32 }, - /// Read header length doesn't match header contents! Length: {size_real}, computed: {size_computed} + /// Read header length doesn't match header contents! Length: `{size_real}`, computed: `{size_computed}` HeaderLengthDoesNotMatchHeaderContents { size_real: u32, size_computed: u32 }, - /// pack size computed from header doesn't match real pack isch! Computed: {size_computed}, real: {size_real} + /// pack size computed from header doesn't match real pack isch! Computed: `{size_computed}`, real: `{size_real}` HeaderPackSizeComputedDoesNotMatchRealPackFile { size_real: u32, size_computed: u32 }, /// partially reading the pack header from packfile failed: `{0:?}` ListingKeyFilesFailed(#[from] BackendAccessErrorKind), @@ -425,20 +431,21 @@ pub enum PackFileErrorKind { PartialReadOfPackfileFailed, /// writing Bytes failed WritingBytesFailed, - /// decryption on backend failed: `{0:?}` + /// [`CryptBackendErrorKind`] + #[error(transparent)] PackDecryptionFailed(#[from] CryptBackendErrorKind), } /// [`SnapshotFileErrorKind`] describes the errors that can be returned for `SnapshotFile`s #[derive(Error, Debug, Display)] pub enum SnapshotFileErrorKind { - /// non-unicode hostname {0:?} + /// non-unicode hostname `{0:?}` NonUnicodeHostname(OsString), - /// non-unicode path {0:?} + /// non-unicode path `{0:?}` NonUnicodePath(PathBuf), /// no snapshots found NoSnapshotsFound, - /// value {0:?} not allowed + /// value `{0:?}` not allowed ValueNotAllowed(String), /// datetime out of range: `{0:?}` OutOfRange(#[from] OutOfRangeError), @@ -450,7 +457,7 @@ pub enum SnapshotFileErrorKind { GettingSnapshotFileByIdFailed, /// unpacking SnapshotFile result failed UnpackingSnapshotFileResultFailed, - /// collecting IDs failed: {0:?} + /// collecting IDs failed: `{0:?}` FindingIdsFailed(Vec), /// removing dots from paths failed: `{0:?}` RemovingDotsFromPathFailed(std::io::Error), @@ -469,11 +476,13 @@ pub enum PackerErrorKind { CompressingDataFailed(#[from] std::io::Error), /// getting total size failed GettingTotalSizeFailed, - /// crossbeam couldn't send message: `{0:?}` + /// [`crossbeam_channel::SendError`] + #[error(transparent)] SendingCrossbeamMessageFailed( #[from] crossbeam_channel::SendError<(bytes::Bytes, BlobId, Option)>, ), - /// crossbeam couldn't send message: `{0:?}` + /// [`crossbeam_channel::SendError`] + #[error(transparent)] SendingCrossbeamMessageFailedForIndexPack( #[from] crossbeam_channel::SendError<(bytes::Bytes, IndexPack)>, ), @@ -487,7 +496,7 @@ pub enum PackerErrorKind { ReadingPartiallyEncryptedDataFailed(#[from] CryptBackendErrorKind), /// failed to partially read data: `{0:?}` PartiallyReadingDataFailed(PackFileErrorKind), - /// failed to add index pack: {0:?} + /// failed to add index pack: `{0:?}` AddingIndexPackFailed(#[from] IndexErrorKind), /// conversion for integer failed: `{0:?}` IntConversionFailed(#[from] TryFromIntError), @@ -496,29 +505,31 @@ pub enum PackerErrorKind { /// [`TreeErrorKind`] describes the errors that can come up dealing with Trees #[derive(Error, Debug, Display)] pub enum TreeErrorKind { - /// blob {0:?} not found in index + /// blob `{0}` not found in index BlobIdNotFound(TreeId), - /// {0:?} is no dir + /// `{0:?}` is not a directory NotADirectory(OsString), - /// "{0:?} not found" + /// Path `{0:?}` not found PathNotFound(OsString), /// path should not contain current or parent dir ContainsCurrentOrParentDirectory, /// serde_json couldn't serialize the tree: `{0:?}` SerializingTreeFailed(#[from] serde_json::Error), - /// serde_json couldn't deserialize tree from bytes of JSON text: {0:?} + /// serde_json couldn't deserialize tree from bytes of JSON text: `{0:?}` DeserializingTreeFailed(serde_json::Error), /// reading blob data failed `{0:?}` ReadingBlobDataFailed(#[from] IndexErrorKind), - /// slice is not UTF-8: {0:?} + /// slice is not UTF-8: `{0:?}` PathIsNotUtf8Conform(#[from] Utf8Error), /// error in building nodestreamer: `{0:?}` BuildingNodeStreamerFailed(#[from] ignore::Error), /// failed to read file string from glob file: `{0:?}` ReadingFileStringFromGlobsFailed(#[from] std::io::Error), - /// crossbeam couldn't send message: `{0:?}` + /// [`crossbeam_channel::SendError`] + #[error(transparent)] SendingCrossbeamMessageFailed(#[from] crossbeam_channel::SendError<(PathBuf, TreeId, usize)>), - /// crossbeam couldn't receive message: `{0:?}` + /// [`crossbeam_channel::RecvError`] + #[error(transparent)] ReceivingCrossbreamMessageFailed(#[from] crossbeam_channel::RecvError), } @@ -527,7 +538,7 @@ pub enum TreeErrorKind { pub enum CacheBackendErrorKind { /// no cache dir NoCacheDirectory, - /// `{0:?}` + /// [`std::io::Error`] #[error(transparent)] FromIoError(#[from] std::io::Error), /// setting option on CacheBackend failed @@ -565,13 +576,13 @@ pub enum CryptBackendErrorKind { WritingDataInCryptBackendFailed, /// failed to list Ids ListingIdsInDecryptionBackendFailed, - /// `{0:?}` + /// [`CryptoErrorKind`] #[error(transparent)] FromKey(#[from] CryptoErrorKind), - /// `{0:?}` + /// [`std::io::Error`] #[error(transparent)] FromIo(#[from] std::io::Error), - /// `{0:?}` + /// [`serde_json::Error`] #[error(transparent)] FromJson(#[from] serde_json::Error), /// writing full hash failed in CryptBackend @@ -595,31 +606,27 @@ pub enum CryptBackendErrorKind { pub enum IgnoreErrorKind { /// generic Ignore error: `{0:?}` GenericError(#[from] ignore::Error), - /// Error reading glob file {file:?}: {source:?} + /// Error reading glob file `{file:?}`: `{source:?}` ErrorGlob { file: PathBuf, - #[source] source: std::io::Error, }, - /// Unable to open file {file:?}: {source:?} + /// Unable to open file `{file:?}`: `{source:?}` UnableToOpenFile { file: PathBuf, - #[source] source: std::io::Error, }, - /// Error getting xattrs for {path:?}: {source:?} + /// Error getting xattrs for `{path:?}`: `{source:?}` ErrorXattr { path: PathBuf, - #[source] source: std::io::Error, }, - /// Error reading link target for {path:?}: {source:?} + /// Error reading link target for `{path:?}`: `{source:?}` ErrorLink { path: PathBuf, - #[source] source: std::io::Error, }, - /// `{0:?}` + /// [`std::num::TryFromIntError`] #[error(transparent)] FromTryFromIntError(#[from] TryFromIntError), } @@ -631,40 +638,37 @@ pub enum LocalDestinationErrorKind { DirectoryCreationFailed(#[from] std::io::Error), /// file `{0:?}` should have a parent FileDoesNotHaveParent(PathBuf), - /// {0:?} + /// [`std::num::TryFromIntError`] #[error(transparent)] FromTryIntError(#[from] TryFromIntError), - /// {0:?} + /// [`IdErrorKind`] #[error(transparent)] FromIdError(#[from] IdErrorKind), - /// {0:?} + /// [`walkdir::Error`] #[error(transparent)] FromWalkdirError(#[from] walkdir::Error), - /// {0:?}# + /// [`Errno`] #[error(transparent)] #[cfg(not(windows))] FromErrnoError(#[from] Errno), - /// listing xattrs on {path:?}: {source:?} + /// listing xattrs on `{path:?}`: `{source:?}` #[cfg(not(any(windows, target_os = "openbsd")))] ListingXattrsFailed { path: PathBuf, - #[source] source: std::io::Error, }, - /// setting xattr {name} on {filename:?} with {source:?} + /// setting xattr `{name}` on `{filename:?}` with `{source:?}` #[cfg(not(any(windows, target_os = "openbsd")))] SettingXattrFailed { name: String, filename: PathBuf, - #[source] source: std::io::Error, }, - /// getting xattr {name} on {filename:?} with {source:?} + /// getting xattr `{name}` on `{filename:?}` with `{source:?}` #[cfg(not(any(windows, target_os = "openbsd")))] GettingXattrFailed { name: String, filename: PathBuf, - #[source] source: std::io::Error, }, /// removing directories failed: `{0:?}` @@ -686,12 +690,11 @@ pub enum LocalDestinationErrorKind { /// setting file permissions failed: `{0:?}` #[cfg(not(windows))] SettingFilePermissionsFailed(std::io::Error), - /// failed to symlink target {linktarget:?} from {filename:?} with {source:?} + /// failed to symlink target `{linktarget:?}` from `{filename:?}` with `{source:?}` #[cfg(not(windows))] SymlinkingFailed { linktarget: PathBuf, filename: PathBuf, - #[source] source: std::io::Error, }, } @@ -699,7 +702,7 @@ pub enum LocalDestinationErrorKind { /// [`NodeErrorKind`] describes the errors that can be returned by an action utilizing a node in Backends #[derive(Error, Debug, Display)] pub enum NodeErrorKind { - /// {0:?} + /// Parsing integer failed: `{0:?}` FromParseIntError(#[from] ParseIntError), /// Unexpected EOF #[cfg(not(windows))] @@ -715,7 +718,7 @@ pub enum NodeErrorKind { /// [`StdInErrorKind`] describes the errors that can be returned while dealing IO from CLI #[derive(Error, Debug, Display)] pub enum StdInErrorKind { - /// StdIn Error: `{0:?}` + /// error reading from stdin: `{0:?}` StdInError(#[from] std::io::Error), } @@ -738,19 +741,19 @@ pub enum ArchiverErrorKind { FailedToSaveFileInBackend(#[from] CryptBackendErrorKind), /// finalizing SnapshotSummary failed: `{0:?}` FinalizingSnapshotSummaryFailed(#[from] SnapshotFileErrorKind), - /// `{0:?}` + /// [`PackerErrorKind`] #[error(transparent)] FromPacker(#[from] PackerErrorKind), - /// `{0:?}` + /// [`TreeErrorKind`] #[error(transparent)] FromTree(#[from] TreeErrorKind), - /// `{0:?}` + /// [`ConfigFileErrorKind`] #[error(transparent)] FromConfigFile(#[from] ConfigFileErrorKind), - /// `{0:?}` + /// [`std::io::Error`] #[error(transparent)] FromStdIo(#[from] std::io::Error), - /// `{0:?}` + /// [`StripPrefixError`] #[error(transparent)] FromStripPrefix(#[from] StripPrefixError), /// conversion from `u64` to `usize` failed: `{0:?}`