#[cfg(feature = "diesel")]
use diesel::r2d2::PoolError;
#[cfg(feature = "diesel")]
use diesel::result::{DatabaseErrorKind, Error as diesel_error};
use std::error::Error;
use std::fmt;
#[cfg(feature = "diesel")]
use crate::error::ConstraintViolationType;
use crate::error::{ConstraintViolationError, InternalError, ResourceTemporarilyUnavailableError};
#[derive(Debug)]
pub enum BatchStoreError {
InternalError(InternalError),
ConstraintViolationError(ConstraintViolationError),
ResourceTemporarilyUnavailableError(ResourceTemporarilyUnavailableError),
NotFoundError(String),
}
impl Error for BatchStoreError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
BatchStoreError::InternalError(err) => Some(err),
BatchStoreError::ConstraintViolationError(err) => Some(err),
BatchStoreError::ResourceTemporarilyUnavailableError(err) => Some(err),
BatchStoreError::NotFoundError(_) => None,
}
}
}
impl fmt::Display for BatchStoreError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BatchStoreError::InternalError(err) => err.fmt(f),
BatchStoreError::ConstraintViolationError(err) => err.fmt(f),
BatchStoreError::ResourceTemporarilyUnavailableError(err) => err.fmt(f),
BatchStoreError::NotFoundError(ref s) => write!(f, "Batch not found: {}", s),
}
}
}
#[cfg(feature = "diesel")]
impl From<diesel_error> for BatchStoreError {
fn from(err: diesel_error) -> BatchStoreError {
match err {
diesel_error::DatabaseError(DatabaseErrorKind::UniqueViolation, _) => {
BatchStoreError::ConstraintViolationError(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::Unique,
Box::new(err),
),
)
}
diesel_error::DatabaseError(DatabaseErrorKind::ForeignKeyViolation, _) => {
BatchStoreError::ConstraintViolationError(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::ForeignKey,
Box::new(err),
),
)
}
_ => BatchStoreError::InternalError(InternalError::from_source(Box::new(err))),
}
}
}
#[cfg(feature = "diesel")]
impl From<PoolError> for BatchStoreError {
fn from(err: PoolError) -> BatchStoreError {
BatchStoreError::ResourceTemporarilyUnavailableError(
ResourceTemporarilyUnavailableError::from_source(Box::new(err)),
)
}
}