use bcrypt::BcryptError;
use std::error::Error;
use std::fmt;
use crate::error::InternalError;
#[derive(Debug)]
pub enum CredentialsStoreError {
OperationError {
context: String,
source: Box<dyn Error>,
},
QueryError {
context: String,
source: Box<dyn Error>,
},
StorageError {
context: String,
source: Option<Box<dyn Error>>,
},
ConnectionError(Box<dyn Error>),
DuplicateError(String),
NotFoundError(String),
InternalError(InternalError),
}
impl Error for CredentialsStoreError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CredentialsStoreError::OperationError { source, .. } => Some(&**source),
CredentialsStoreError::QueryError { source, .. } => Some(&**source),
CredentialsStoreError::StorageError {
source: Some(source),
..
} => Some(&**source),
CredentialsStoreError::StorageError { source: None, .. } => None,
CredentialsStoreError::ConnectionError(err) => Some(&**err),
CredentialsStoreError::DuplicateError(_) => None,
CredentialsStoreError::NotFoundError(_) => None,
CredentialsStoreError::InternalError(source) => Some(source),
}
}
}
impl fmt::Display for CredentialsStoreError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CredentialsStoreError::OperationError { context, source } => {
write!(f, "failed to perform operation: {}: {}", context, source)
}
CredentialsStoreError::QueryError { context, source } => {
write!(f, "failed query: {}: {}", context, source)
}
CredentialsStoreError::StorageError {
context,
source: Some(source),
} => write!(
f,
"the underlying storage returned an error: {}: {}",
context, source
),
CredentialsStoreError::StorageError {
context,
source: None,
} => write!(f, "the underlying storage returned an error: {}", context,),
CredentialsStoreError::ConnectionError(ref s) => {
write!(f, "failed to connect to underlying storage: {}", s)
}
CredentialsStoreError::DuplicateError(ref s) => {
write!(f, "credentials already exists: {}", s)
}
CredentialsStoreError::NotFoundError(ref s) => {
write!(f, "credentials not found: {}", s)
}
CredentialsStoreError::InternalError(e) => f.write_str(&e.to_string()),
}
}
}
#[cfg(feature = "diesel")]
impl From<diesel::r2d2::PoolError> for CredentialsStoreError {
fn from(err: diesel::r2d2::PoolError) -> CredentialsStoreError {
CredentialsStoreError::ConnectionError(Box::new(err))
}
}
impl From<InternalError> for CredentialsStoreError {
fn from(err: InternalError) -> Self {
Self::InternalError(err)
}
}
#[derive(Debug)]
pub enum CredentialsBuilderError {
MissingRequiredField(String),
BuildError(Box<dyn Error>),
EncryptionError(Box<dyn Error>),
}
impl Error for CredentialsBuilderError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CredentialsBuilderError::MissingRequiredField(_) => None,
CredentialsBuilderError::BuildError(err) => Some(&**err),
CredentialsBuilderError::EncryptionError(err) => Some(&**err),
}
}
}
impl fmt::Display for CredentialsBuilderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CredentialsBuilderError::MissingRequiredField(ref s) => {
write!(f, "failed to build user credentials: {}", s)
}
CredentialsBuilderError::BuildError(ref s) => {
write!(f, "failed to build credentials: {}", s)
}
CredentialsBuilderError::EncryptionError(ref s) => {
write!(f, "failed encrypt password: {}", s)
}
}
}
}
impl From<BcryptError> for CredentialsBuilderError {
fn from(err: BcryptError) -> CredentialsBuilderError {
CredentialsBuilderError::EncryptionError(Box::new(err))
}
}
#[derive(Debug)]
pub enum CredentialsError {
VerificationError(Box<dyn Error>),
}
impl Error for CredentialsError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CredentialsError::VerificationError(err) => Some(&**err),
}
}
}
impl fmt::Display for CredentialsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CredentialsError::VerificationError(ref s) => {
write!(f, "failed to verify password: {}", s)
}
}
}
}
impl From<BcryptError> for CredentialsError {
fn from(err: BcryptError) -> CredentialsError {
CredentialsError::VerificationError(Box::new(err))
}
}