use std::error::Error;
use std::fmt;
#[cfg(feature = "diesel")]
use crate::error::ConstraintViolationType;
use crate::error::{
ConstraintViolationError, InternalError, InvalidArgumentError, InvalidStateError,
};
#[derive(Debug)]
pub enum OAuthUserSessionStoreError {
ConstraintViolation(ConstraintViolationError),
Internal(InternalError),
InvalidArgument(InvalidArgumentError),
InvalidState(InvalidStateError),
}
impl Error for OAuthUserSessionStoreError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
OAuthUserSessionStoreError::ConstraintViolation(err) => err.source(),
OAuthUserSessionStoreError::Internal(err) => err.source(),
OAuthUserSessionStoreError::InvalidArgument(err) => err.source(),
OAuthUserSessionStoreError::InvalidState(err) => err.source(),
}
}
}
impl fmt::Display for OAuthUserSessionStoreError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
OAuthUserSessionStoreError::ConstraintViolation(err) => f.write_str(&err.to_string()),
OAuthUserSessionStoreError::Internal(err) => f.write_str(&err.to_string()),
OAuthUserSessionStoreError::InvalidArgument(err) => f.write_str(&err.to_string()),
OAuthUserSessionStoreError::InvalidState(err) => f.write_str(&err.to_string()),
}
}
}
#[cfg(feature = "diesel")]
impl From<diesel::r2d2::PoolError> for OAuthUserSessionStoreError {
fn from(err: diesel::r2d2::PoolError) -> Self {
OAuthUserSessionStoreError::Internal(InternalError::from_source(Box::new(err)))
}
}
#[cfg(feature = "diesel")]
impl From<diesel::result::Error> for OAuthUserSessionStoreError {
fn from(err: diesel::result::Error) -> Self {
match err {
diesel::result::Error::DatabaseError(ref kind, _) => match kind {
diesel::result::DatabaseErrorKind::UniqueViolation => {
OAuthUserSessionStoreError::ConstraintViolation(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::Unique,
Box::new(err),
),
)
}
diesel::result::DatabaseErrorKind::ForeignKeyViolation => {
OAuthUserSessionStoreError::ConstraintViolation(
ConstraintViolationError::from_source_with_violation_type(
ConstraintViolationType::ForeignKey,
Box::new(err),
),
)
}
_ => {
OAuthUserSessionStoreError::Internal(InternalError::from_source(Box::new(err)))
}
},
_ => OAuthUserSessionStoreError::Internal(InternalError::from_source(Box::new(err))),
}
}
}
impl From<InternalError> for OAuthUserSessionStoreError {
fn from(err: InternalError) -> Self {
Self::Internal(err)
}
}