From a33d1be66c0c625f50e5a74f1e9c18bd043bd598 Mon Sep 17 00:00:00 2001 From: Alexander Fadeev Date: Mon, 27 May 2024 15:55:37 +0300 Subject: [PATCH] Implement std::error::Error for cocoon::Error via optional `thiserror` (#28) This adds the `std::error::Error` trait implementation to `cocoon::Error` using the `thiserror` crate. Note that this is only limited to the presence of feature "thiserror". Co-authored-by: Alexander Fadeev Co-authored-by: madomado --- Cargo.toml | 1 + src/error.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 31b8759..f45e2ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ hmac = "0.11" pbkdf2 = {version = "0.9", default-features = false, features = ["sha2", "hmac"]} rand = {version = "0.8", default-features = false, features = ["std_rng"]} sha2 = {version = "0.9", default-features = false} +thiserror = {version = "1.0.61", optional = true} zeroize = {version = "1", default-features = false} [dev-dependencies] diff --git a/src/error.rs b/src/error.rs index 8b7c397..c7102db 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,20 +1,39 @@ /// Error variants produced by the Cocoon API. #[derive(Debug)] +#[cfg_attr(feature = "thiserror", derive(thiserror::Error))] pub enum Error { /// I/o error during read/write operation (`Cocoon::dump`, `Cocoon::parse`). #[cfg(feature = "std")] + #[cfg_attr(feature = "thiserror", error("Input/output error"))] Io(std::io::Error), + /// Format is not recognized. Probably corrupted. + #[cfg_attr(feature = "thiserror", error("Unrecognized format"))] UnrecognizedFormat, + /// Cryptographic error. There could be a few reasons: /// 1. Integrity is compromised. /// 2. Password is invalid. + #[cfg_attr( + feature = "thiserror", + error("Cryptographic error: bad integrity/password") + )] Cryptography, + /// Container is too large to get processed on the current architecture. /// E.g. it's not possible to process a container larger than 4 GB on 32-bit architecture. + #[cfg_attr( + feature = "thiserror", + error("Container size exceeds architectural limit") + )] TooLarge, + /// Buffer is too short and barely holds all data to decrypt, inconsistent length /// encoded to the header. + #[cfg_attr( + feature = "thiserror", + error("Insufficient buffer size for decrypted data") + )] TooShort, }