use std::fmt;
pub type SalesforceAuthResult<T> = Result<T, SalesforceAuthError>;
#[derive(Debug)]
pub enum SalesforceAuthError {
Config(String),
PrivateKey(String),
Jwt(String),
Http(String),
Authorization {
error_code: String,
error_description: String,
},
TokenExchange(String),
TokenParse(String),
TokenExpired,
Io(String),
}
impl fmt::Display for SalesforceAuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SalesforceAuthError::Config(msg) => write!(f, "configuration error: {msg}"),
SalesforceAuthError::PrivateKey(msg) => write!(f, "private key error: {msg}"),
SalesforceAuthError::Jwt(msg) => write!(f, "JWT assertion error: {msg}"),
SalesforceAuthError::Http(msg) => write!(f, "HTTP error: {msg}"),
SalesforceAuthError::Authorization {
error_code,
error_description,
} => write!(
f,
"authorization failed: {error_code} - {error_description}"
),
SalesforceAuthError::TokenExchange(msg) => {
write!(f, "DC JWT exchange failed: {msg}")
}
SalesforceAuthError::TokenParse(msg) => write!(f, "token parse error: {msg}"),
SalesforceAuthError::TokenExpired => write!(f, "DC JWT has expired"),
SalesforceAuthError::Io(msg) => write!(f, "I/O error: {msg}"),
}
}
}
impl std::error::Error for SalesforceAuthError {}
impl From<reqwest::Error> for SalesforceAuthError {
fn from(err: reqwest::Error) -> Self {
SalesforceAuthError::Http(err.to_string())
}
}
impl From<jsonwebtoken::errors::Error> for SalesforceAuthError {
fn from(err: jsonwebtoken::errors::Error) -> Self {
SalesforceAuthError::Jwt(err.to_string())
}
}
impl From<rsa::pkcs8::Error> for SalesforceAuthError {
fn from(err: rsa::pkcs8::Error) -> Self {
SalesforceAuthError::PrivateKey(err.to_string())
}
}
impl From<url::ParseError> for SalesforceAuthError {
fn from(err: url::ParseError) -> Self {
SalesforceAuthError::Config(format!("invalid URL: {err}"))
}
}
impl From<serde_json::Error> for SalesforceAuthError {
fn from(err: serde_json::Error) -> Self {
SalesforceAuthError::TokenParse(err.to_string())
}
}