mongor 0.1.10

Ergonomic MongoDB ODM
Documentation
// Authors: Robert Lopez
// License: MIT (See `LICENSE.md`)

use std::fmt;

/// Error enum to wrap various errors that can occur inside the crate.
///
/// SharedSessionLockFailure holds a stringified version of the poison
/// error.
#[derive(Debug, Clone)]
pub enum Error {
    Internal(String),
    IO(std::io::ErrorKind),
    Bson(mongodb::bson::de::Error),
    Mongo(mongodb::error::Error),
    TestError(String),
    SharedSessionLockFailure(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Internal(err) => write!(f, "Internal Error: {err}"),
            Self::IO(err) => write!(f, "IO Error: {:?}", err),
            Self::Bson(err) => write!(f, "Bson Error: {:?}", err),
            Self::Mongo(err) => write!(f, "Mongo Error: {:?}", err),
            Self::TestError(err) => write!(f, "Test Error: {err}"),
            Self::SharedSessionLockFailure(err) => {
                write!(f, "SharedSessionLockFailure Error: {err}")
            }
        }
    }
}

impl std::error::Error for Error {}

impl Error {
    pub fn internal(message: &str) -> Self {
        Self::Internal(message.to_string())
    }
}