use std::error::Error;
use std::fmt;
use std::time::Duration;
#[derive(Debug)]
pub struct CredentialsNotLoaded {
source: Option<Box<dyn Error + Send + Sync + 'static>>,
}
#[derive(Debug)]
pub struct ProviderTimedOut {
timeout_duration: Duration,
}
impl ProviderTimedOut {
pub fn timeout_duration(&self) -> Duration {
self.timeout_duration
}
}
#[derive(Debug)]
pub struct InvalidConfiguration {
source: Box<dyn Error + Send + Sync + 'static>,
}
#[derive(Debug)]
pub struct ProviderError {
source: Box<dyn Error + Send + Sync + 'static>,
}
#[derive(Debug)]
pub struct Unhandled {
source: Box<dyn Error + Send + Sync + 'static>,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum CredentialsError {
CredentialsNotLoaded(CredentialsNotLoaded),
ProviderTimedOut(ProviderTimedOut),
InvalidConfiguration(InvalidConfiguration),
ProviderError(ProviderError),
Unhandled(Unhandled),
}
impl CredentialsError {
pub fn not_loaded(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
CredentialsError::CredentialsNotLoaded(CredentialsNotLoaded {
source: Some(source.into()),
})
}
pub fn not_loaded_no_source() -> Self {
CredentialsError::CredentialsNotLoaded(CredentialsNotLoaded { source: None })
}
pub fn unhandled(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::Unhandled(Unhandled {
source: source.into(),
})
}
pub fn provider_error(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::ProviderError(ProviderError {
source: source.into(),
})
}
pub fn invalid_configuration(
source: impl Into<Box<dyn Error + Send + Sync + 'static>>,
) -> Self {
Self::InvalidConfiguration(InvalidConfiguration {
source: source.into(),
})
}
pub fn provider_timed_out(timeout_duration: Duration) -> Self {
Self::ProviderTimedOut(ProviderTimedOut { timeout_duration })
}
}
impl fmt::Display for CredentialsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CredentialsError::CredentialsNotLoaded(_) => {
write!(f, "the credential provider was not enabled")
}
CredentialsError::ProviderTimedOut(details) => write!(
f,
"credentials provider timed out after {} seconds",
details.timeout_duration.as_secs()
),
CredentialsError::InvalidConfiguration(_) => {
write!(f, "the credentials provider was not properly configured")
}
CredentialsError::ProviderError(_) => {
write!(f, "an error occurred while loading credentials")
}
CredentialsError::Unhandled(_) => {
write!(f, "unexpected credentials error")
}
}
}
}
impl Error for CredentialsError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CredentialsError::CredentialsNotLoaded(details) => {
details.source.as_ref().map(|s| s.as_ref() as _)
}
CredentialsError::ProviderTimedOut(_) => None,
CredentialsError::InvalidConfiguration(details) => Some(details.source.as_ref() as _),
CredentialsError::ProviderError(details) => Some(details.source.as_ref() as _),
CredentialsError::Unhandled(details) => Some(details.source.as_ref() as _),
}
}
}
#[derive(Debug)]
pub struct TokenNotLoaded {
source: Box<dyn Error + Send + Sync + 'static>,
}
#[derive(Debug)]
pub enum TokenError {
TokenNotLoaded(TokenNotLoaded),
ProviderTimedOut(ProviderTimedOut),
InvalidConfiguration(InvalidConfiguration),
ProviderError(ProviderError),
Unhandled(Unhandled),
}
impl TokenError {
pub fn not_loaded(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
TokenError::TokenNotLoaded(TokenNotLoaded {
source: source.into(),
})
}
pub fn unhandled(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::Unhandled(Unhandled {
source: source.into(),
})
}
pub fn provider_error(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::ProviderError(ProviderError {
source: source.into(),
})
}
pub fn invalid_configuration(
source: impl Into<Box<dyn Error + Send + Sync + 'static>>,
) -> Self {
Self::InvalidConfiguration(InvalidConfiguration {
source: source.into(),
})
}
pub fn provider_timed_out(timeout_duration: Duration) -> Self {
Self::ProviderTimedOut(ProviderTimedOut { timeout_duration })
}
}
impl fmt::Display for TokenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TokenError::TokenNotLoaded(_) => {
write!(f, "the access token provider was not enabled")
}
TokenError::ProviderTimedOut(details) => write!(
f,
"access token provider timed out after {} seconds",
details.timeout_duration.as_secs()
),
TokenError::InvalidConfiguration(_) => {
write!(f, "the access token provider was not properly configured")
}
TokenError::ProviderError(_) => {
write!(f, "an error occurred while loading an access token")
}
TokenError::Unhandled(_) => {
write!(f, "unexpected access token providererror")
}
}
}
}
impl Error for TokenError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
TokenError::TokenNotLoaded(details) => Some(details.source.as_ref() as _),
TokenError::ProviderTimedOut(_) => None,
TokenError::InvalidConfiguration(details) => Some(details.source.as_ref() as _),
TokenError::ProviderError(details) => Some(details.source.as_ref() as _),
TokenError::Unhandled(details) => Some(details.source.as_ref() as _),
}
}
}