Skip to content
errors.rs 2.08 KiB
Newer Older
pub type CvfsResult<O> = Result<O, CvfsErrors>;

Stefan Schindler's avatar
Stefan Schindler committed
#[derive(Debug, PartialEq)]
pub enum CvfsErrors {
    KeyManagementError(multi_key_manager::errors::KeyManagementError),
Stefan Schindler's avatar
Stefan Schindler committed

    /// An error occured inside ring, but this was the specific action
    LeafDecryptionError,

    /// Maybe the main Cvfs was dropped or the specific File deleted before?
    FileHandlerNotFound(crate::metadata::FileTag),

Stefan Schindler's avatar
Stefan Schindler committed
    /// Std::io::Error
    GeneralIO(String),
    /// Std::io::Error with a reason
    ContextIO(&'static str, String),
    /// A constant time error occured in the `ring` crate
    GeneralRingEncryptionError,
Stefan Schindler's avatar
Stefan Schindler committed
    MutexUnlockError(String),

    /// A serialisation error occured
    RmpEncode(String),
    /// A deserialisation error occured
    RmpDecode(String),
pub fn specify_io_error<O>(r: Result<O, std::io::Error>, reason: &'static str) -> CvfsResult<O> {
    r.map_err(|e| CvfsErrors::ContextIO(reason, format!("{e:?}")))
}
pub fn specify_kme_error<O>(
    r: Result<O, multi_key_manager::errors::KeyManagementError>,
    reason: &'static str,
) -> CvfsResult<O> {
    r.map_err(|e| CvfsErrors::ContextIO(reason, format!("{e:?}")))
}

impl From<std::io::Error> for CvfsErrors {
    fn from(e: std::io::Error) -> Self {
        CvfsErrors::GeneralIO(format!("{e:?}"))
    }
}

impl From<multi_key_manager::errors::KeyManagementError> for CvfsErrors {
    fn from(e: multi_key_manager::errors::KeyManagementError) -> Self {
        CvfsErrors::KeyManagementError(e)
    }
}

impl From<ring::error::Unspecified> for CvfsErrors {
    fn from(_: ring::error::Unspecified) -> Self {
        CvfsErrors::GeneralRingEncryptionError
Stefan Schindler's avatar
Stefan Schindler committed
impl<T> From<std::sync::PoisonError<T>> for CvfsErrors {
    fn from(e: std::sync::PoisonError<T>) -> Self {
        CvfsErrors::MutexUnlockError(format!("{e:?}"))
    }
}

impl From<rmp_serde::encode::Error> for CvfsErrors {
    fn from(e: rmp_serde::encode::Error) -> Self {
        CvfsErrors::RmpEncode(format!("{e:?}"))
    }
}
impl From<rmp_serde::decode::Error> for CvfsErrors {
    fn from(e: rmp_serde::decode::Error) -> Self {
        CvfsErrors::RmpDecode(format!("{e:?}"))
    }
}