1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::storage::MailstromStorageError;
use crate::worker::Message;
use email_format::rfc5322::ParseError;
use std::convert::From;
use std::io::Error as IoError;
use std::sync::mpsc::SendError;

#[derive(Debug)]
pub enum Error {
    Send(SendError<Message>),
    EmailParser(ParseError),
    General(String),
    Storage(String),
    DnsUnavailable,
    Lock,
    Io(IoError),
    LettreEmailAddress(lettre::error::Error),
}

impl From<SendError<Message>> for Error {
    fn from(e: SendError<Message>) -> Error {
        Error::Send(e)
    }
}

impl From<ParseError> for Error {
    fn from(e: ParseError) -> Error {
        Error::EmailParser(e)
    }
}

impl From<String> for Error {
    fn from(e: String) -> Error {
        Error::General(e)
    }
}

impl<S: MailstromStorageError> From<S> for Error {
    fn from(e: S) -> Error {
        Error::Storage(format!("{}", e))
    }
}

impl From<IoError> for Error {
    fn from(e: IoError) -> Error {
        Error::Io(e)
    }
}

impl ::std::fmt::Display for Error {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        use std::error::Error as StdError;

        match *self {
            Error::Send(ref e) => format!("{}: {:?}", self.description(), e).fmt(f),
            Error::EmailParser(ref e) => format!("{}: {:?}", self.description(), e).fmt(f),
            Error::General(ref e) => format!("{}: {}", self.description(), e).fmt(f),
            Error::Storage(ref s) => format!("{}: {}", self.description(), s).fmt(f),
            Error::LettreEmailAddress(ref e) => format!("{}: {}", self.description(), e).fmt(f),
            _ => self.description().to_string().fmt(f),
        }
    }
}

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Send(_) => "Unable to send message to worker",
            Error::EmailParser(_) => "Email does not parse",
            Error::General(_) => "General error",
            Error::Storage(_) => "Could not store or retrieve email state data",
            Error::DnsUnavailable => "DNS unavailable",
            Error::Lock => "Lock poisoned",
            Error::Io(_) => "I/O error",
            Error::LettreEmailAddress(_) => "Lettre crate Email Address error",
        }
    }

    fn cause(&self) -> Option<&dyn ::std::error::Error> {
        match *self {
            Error::Send(ref e) => Some(e),
            Error::EmailParser(ref e) => Some(e),
            Error::Io(ref e) => Some(e),
            _ => None,
        }
    }
}