mail_smtp/
error.rs

1//! Module containing all custom errors.
2use std::{io as std_io};
3
4use new_tokio_smtp::error::{
5    ConnectingFailed,
6    LogicError, GeneralError
7};
8
9use mail::error::MailError;
10use headers::error::HeaderValidationError;
11
12/// Error used when sending a mail fails.
13///
14/// Failing to encode a mail before sending
15/// it also counts as a `MailSendError`, as
16/// it's done "on the fly" when sending a mail.
17#[derive(Debug, Fail)]
18pub enum MailSendError {
19
20    /// Something is wrong with the mail instance (e.g. it can't be encoded).
21    ///
22    /// This can happen for a number of reasons including:
23    ///
24    /// 1. Missing header fields.
25    /// 2. Invalid header fields.
26    /// 2. Encoding header fields fails.
27    /// 3. Loading resources failed (resources like e.g. appendix, logo embedded in html mail, etc.)
28    #[fail(display = "{}", _0)]
29    Mail(MailError),
30
31    /// Sending the mail failed.
32    ///
33    /// This can happen for a number of reasons including:
34    /// 1. Server rejects mail transaction because of send or receiver
35    ///    address or body data (e.g. body to long).
36    /// 2. Mail address requires smtputf8 support, which is not given.
37    /// 3. Server rejects sending the mail for other reasons (it's
38    ///    closing, overloaded etc.).
39    #[fail(display = "{}", _0)]
40    Smtp(LogicError),
41
42    /// Setting up the connection failed.
43    ///
44    /// Failures can include but are not limited to:
45    ///
46    /// - Connecting with TCP failed.
47    /// - Starting TLS failed.
48    /// - Server does not want to be used (e.g. failure on sending EHLO).
49    /// - Authentication failed.
50    #[fail(display = "{}", _0)]
51    Connecting(ConnectingFailed),
52
53    /// An I/O error happened while using the connection.
54    ///
55    /// This is mainly for I/O errors after the setup of the connection
56    /// was successful, which normally includes sending Ehlo and Auth
57    /// commands.
58    #[fail(display = "{}", _0)]
59    Io(std_io::Error)
60}
61
62impl From<MailError> for MailSendError {
63    fn from(err: MailError) -> Self {
64        MailSendError::Mail(err)
65    }
66}
67
68impl From<LogicError> for MailSendError {
69    fn from(err: LogicError) -> Self {
70        MailSendError::Smtp(err)
71    }
72}
73
74impl From<std_io::Error> for MailSendError {
75    fn from(err: std_io::Error) -> Self {
76        MailSendError::Io(err)
77    }
78}
79
80impl From<ConnectingFailed> for MailSendError {
81    fn from(err: ConnectingFailed) -> Self {
82        MailSendError::Connecting(err)
83    }
84}
85
86impl From<GeneralError> for MailSendError {
87    fn from(err: GeneralError) -> Self {
88        use self::GeneralError::*;
89        match err {
90            Connecting(err) => Self::from(err),
91            Cmd(err) => Self::from(err),
92            Io(err) => Self::from(err)
93        }
94    }
95}
96
97
98#[derive(Debug, Fail)]
99pub enum OtherValidationError {
100
101    #[fail(display = "no To header was present")]
102    NoTo
103}
104
105impl From<OtherValidationError> for HeaderValidationError {
106
107    fn from(ove: OtherValidationError) -> Self {
108        HeaderValidationError::Custom(ove.into())
109    }
110}
111
112impl From<OtherValidationError> for MailError {
113    fn from(ove: OtherValidationError) -> Self {
114        MailError::from(HeaderValidationError::from(ove))
115    }
116}