Skip to content

Commit

Permalink
Implement std::error::Error for cocoon::Error via optional thiserror (
Browse files Browse the repository at this point in the history
#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 <[email protected]>
Co-authored-by: madomado <[email protected]>
  • Loading branch information
fadeevab and madonuko authored May 27, 2024
1 parent 0d67106 commit a33d1be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 19 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down

0 comments on commit a33d1be

Please sign in to comment.