Skip to main content

capy_mail/
error.rs

1/// Safe error categories. Raw SMTP responses/credentials/content are never retained.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum MailError {
4    /// Invalid or unsafe configuration.
5    Configuration,
6    /// Invalid mailbox syntax.
7    Address,
8    /// Invalid header or control characters.
9    Header,
10    /// Invalid message structure.
11    Message,
12    /// Invalid attachment metadata or size.
13    Attachment,
14    /// Message exceeds configured bounds.
15    Size,
16    /// Template rendering failed.
17    Template,
18    /// Connection or protocol failure, conservatively retryable.
19    Connection,
20    /// Authentication rejected; operator action required.
21    Authentication,
22    /// TLS negotiation or certificate validation failed.
23    Tls,
24    /// SMTP temporary failure.
25    Temporary,
26    /// SMTP permanent failure.
27    Permanent,
28    /// Total send deadline expired; delivery may already have happened.
29    Timeout,
30    /// Test capture reached its explicit limit.
31    CaptureFull,
32    /// Private development inbox I/O, permissions or serialization failed.
33    CaptureStorage,
34    /// Queue persistence failed; commit status can be ambiguous.
35    Queue,
36}
37impl MailError {
38    /// Stable category for safe observability.
39    pub fn category(self) -> &'static str {
40        match self {
41            Self::Configuration => "configuration",
42            Self::Address => "address",
43            Self::Header => "header",
44            Self::Message => "message",
45            Self::Attachment => "attachment",
46            Self::Size => "size",
47            Self::Template => "template",
48            Self::Connection => "connection",
49            Self::Authentication => "authentication",
50            Self::Tls => "tls",
51            Self::Temporary => "temporary",
52            Self::Permanent => "permanent",
53            Self::Timeout => "timeout",
54            Self::CaptureFull => "capture_full",
55            Self::CaptureStorage => "capture_storage",
56            Self::Queue => "queue",
57        }
58    }
59    /// Only temporary/connection/deadline errors are automatically retryable by Jobs.
60    pub fn retryable(self) -> bool {
61        matches!(self, Self::Temporary | Self::Connection | Self::Timeout)
62    }
63}
64impl std::fmt::Display for MailError {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "Mail operation failed ({})", self.category())
67    }
68}
69impl std::error::Error for MailError {}
70impl From<askama::Error> for MailError {
71    fn from(_: askama::Error) -> Self {
72        Self::Template
73    }
74}
75impl From<MailError> for capy_core::AppError {
76    fn from(error: MailError) -> Self {
77        Self::Internal(error.into())
78    }
79}
80
81impl From<MailError> for capy_core::EventError {
82    fn from(error: MailError) -> Self {
83        Self::Failure(capy_core::AppError::from(error))
84    }
85}