Skip to main content

pq_objectstore/
error.rs

1//! Crate-owned error types for `pq-objectstore`.
2
3use std::fmt;
4
5/// Convenient result alias for this crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors produced by encrypted object storage operations.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// The underlying object store rejected or failed an operation.
13    #[error("object backend error: {0}")]
14    Backend(String),
15
16    /// Cryptographic operation failed.
17    #[error("cryptographic error: {0}")]
18    Crypto(String),
19
20    /// The object header could not be parsed.
21    #[error("invalid object header: {0}")]
22    InvalidHeader(String),
23
24    /// The object format version is not supported by this build.
25    #[error("unsupported object format version: {0}")]
26    UnsupportedVersion(u16),
27
28    /// The cipher suite is not supported by this build.
29    #[error("unsupported cipher suite: {0}")]
30    UnsupportedSuite(u16),
31
32    /// The referenced key ID is unknown to the configured [`crate::KeyProvider`].
33    #[error("unknown key id: {0}")]
34    UnknownKey(String),
35
36    /// Authenticated decryption failed (tampering or wrong key).
37    #[error("authentication failed")]
38    AuthenticationFailed,
39
40    /// I/O failure while reading or writing object data.
41    #[error("I/O error: {0}")]
42    Io(#[from] std::io::Error),
43
44    /// Invalid configuration supplied to a builder.
45    #[error("invalid configuration: {0}")]
46    Config(String),
47}
48
49impl Error {
50    #[cfg(feature = "store")]
51    pub(crate) fn backend(err: impl fmt::Display) -> Self {
52        Self::Backend(err.to_string())
53    }
54
55    pub(crate) fn crypto(err: impl fmt::Display) -> Self {
56        Self::Crypto(err.to_string())
57    }
58
59    pub(crate) fn invalid_header(err: impl fmt::Display) -> Self {
60        Self::InvalidHeader(err.to_string())
61    }
62
63    pub(crate) fn config(err: impl fmt::Display) -> Self {
64        Self::Config(err.to_string())
65    }
66}
67
68/// Wrap a crate error so it can travel through [`std::io::Error`].
69#[cfg(feature = "store")]
70pub(crate) fn io_error(err: Error) -> std::io::Error {
71    std::io::Error::new(std::io::ErrorKind::InvalidData, err)
72}
73
74/// Recover a crate error stored by [`io_error`], or keep a plain I/O failure.
75#[cfg(feature = "store")]
76pub(crate) fn error_from_io(err: std::io::Error) -> Error {
77    match err.downcast::<Error>() {
78        Ok(inner) => inner,
79        Err(err) => Error::Io(err),
80    }
81}