use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum MailError {
#[error("Email provider not configured")]
NotConfigured,
#[error("Configuration error: {0}")]
Configuration(String),
#[error("Missing required field: {0}")]
MissingField(&'static str),
#[error("Invalid email address: {0}")]
InvalidAddress(String),
#[error("Attachment error: {0}")]
AttachmentError(String),
#[error("Attachment has no content: {0}")]
AttachmentMissingContent(String),
#[error("Attachment file not found: {0}")]
AttachmentFileNotFound(String),
#[error("Failed to read attachment: {0}")]
AttachmentReadError(String),
#[error("Build error: {0}")]
BuildError(String),
#[error("Send error: {0}")]
SendError(String),
#[error("Unsupported feature: {0}")]
UnsupportedFeature(String),
#[error("Provider error ({provider}): {message}")]
ProviderError {
provider: &'static str,
message: String,
status: Option<u16>,
},
#[error("HTTP error: {0}")]
HttpError(String),
#[error("JSON error: {0}")]
JsonError(String),
#[error("Template error: {0}")]
TemplateError(String),
#[error("Internal error: {0}")]
Internal(String),
}
impl MailError {
pub fn provider(provider: &'static str, message: impl Into<String>) -> Self {
Self::ProviderError {
provider,
message: message.into(),
status: None,
}
}
pub fn provider_with_status(
provider: &'static str,
message: impl Into<String>,
status: u16,
) -> Self {
Self::ProviderError {
provider,
message: message.into(),
status: Some(status),
}
}
}
#[cfg(feature = "_http")]
impl From<reqwest::Error> for MailError {
fn from(err: reqwest::Error) -> Self {
Self::HttpError(err.to_string())
}
}
impl From<serde_json::Error> for MailError {
fn from(err: serde_json::Error) -> Self {
Self::JsonError(err.to_string())
}
}
#[cfg(feature = "smtp")]
impl From<lettre::error::Error> for MailError {
fn from(err: lettre::error::Error) -> Self {
Self::SendError(err.to_string())
}
}
#[cfg(feature = "smtp")]
impl From<lettre::transport::smtp::Error> for MailError {
fn from(err: lettre::transport::smtp::Error) -> Self {
Self::SendError(err.to_string())
}
}
#[cfg(feature = "smtp")]
impl From<lettre::address::AddressError> for MailError {
fn from(err: lettre::address::AddressError) -> Self {
Self::InvalidAddress(err.to_string())
}
}