summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: f22f331b4da5bfddfc2bc87a252db2ffdb087442 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Module contains all custom errors introduced by this crate.
use std::fmt::{self, Display, Debug};
use std::mem::drop;

use failure::{Fail, Context, Backtrace};

use headers::error::{
    HeaderTypeError,
    ComponentCreationError
};
use mail::error::{BuilderError, OtherBuilderErrorKind};


/*
error cases:

- MailError
    - resource Loading
    - encoding
    - component creation
    - header validation

- Template
    - loading?
    - using?

*/

/// Extension adding a  `with_source` method to anything implementing `Fail`.
///
/// This provides a uniform way to combine a error with the source which
/// caused the error and return them together. The returned `WithSource`
/// instance will implement `Fail` if possible but does not require it,
/// making it compatible with non 'static, Send, Sync sources.
pub trait WithSourceExt:  Sized + Fail {
    fn with_source<S>(self, source: S) -> WithSource<Self, S>
        where S: Debug
    {
        WithSource::new(self, source)
    }
}

impl<FT> WithSourceExt for FT
    where FT: Fail
{}

/// Combines a error with a source into a type potentially implementing Fail
///
/// Fail is implemented if the Source if `Send`, `Sync` and `'static`.
/// The Display impl always forwards to the contained error as the
/// source might not implement `Display`
#[derive(Debug)]
pub struct WithSource<E: Fail, S: Debug> {
    error: E,
    source: S,
}

impl<E, S> WithSource<E, S>
    where E: Fail, S: Debug
{

    /// Create a new instance given an error and it's source.
    pub fn new(error: E, source: S) -> Self {
        WithSource { error, source }
    }

    /// Return a reference to it's source.
    pub fn source(&self) -> &S {
        &self.source
    }

    /// Return a reference to the contained error.
    pub fn error(&self) -> &E {
        &self.error
    }

    /// Turns this type into it's source.
    pub fn into_source(self) -> S {
        let WithSource { error, source } = self;
        drop(error);
        source
    }

    /// Turns this type into it's error.
    pub fn into_error(self) -> E {
        let WithSource { error, source } = self;
        drop(source);
        error
    }

    /// Decomposes this type into its error and its source.
    pub fn split(self) -> (E, S) {
        let WithSource { error, source } = self;
        (error, source)
    }
}

impl<E, S> Fail for WithSource<E, S>
    where E: Fail, S: Debug + Send + Sync + 'static
{
    fn cause(&self) -> Option<&Fail> {
        self.error.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.error.backtrace()
    }
}

impl<E, S> Display for WithSource<E, S>
    where E: Fail, S: Debug
{
    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(self.error(), fter)
    }
}

/// Error returned when composing a `Mail` failed.
#[derive(Debug, Fail)]
pub enum CompositionError<TE: Fail> {
    /// It failed to use the underlying template engine.
    #[fail(display = "{}", _0)]
    Template(TE),

    /// It didn't fail to get all `MailParts` but wasn't
    /// able to compose them into an mail.
    #[fail(display = "{}", _0)]
    Builder(ExtendedBuilderError)
}

impl<FT, TE> From<FT> for CompositionError<TE>
    where TE: Fail, ExtendedBuilderError: From<FT>
{
    fn from(error: FT) -> Self {
        CompositionError::Builder(error.into())
    }
}

// impl<TE> From<ExtendedBuilderError> for CompositionError<TE>
//     where TE: Fail
// {
//     fn from(error: ExtendedBuilderError) -> Self {
//         CompositionError::Builder(error)
//     }
// }

/// Kinds of Error which can be caused by the builder extension.
///
/// (The builder extension is a trait providing additional methods
///  to the `MailBuilder`)
#[derive(Copy, Clone, Debug, Fail, PartialEq, Eq, Hash)]
pub enum ExtendedBuilderErrorKind {
    #[fail(display="need embedding to create a body with an embedding")]
    EmbeddingMissing,
}

/// Error returned if building the mail failed.
#[derive(Debug, Fail)]
pub enum ExtendedBuilderError {
    /// A error covered by `BuilderError` occurred.
    #[fail(display = "{}", _0)]
    Normal(BuilderError),

    /// An error not covered by `BuilderError` occurred.
    #[fail(display = "{}", _0)]
    Extended(Context<ExtendedBuilderErrorKind>)

}

impl From<BuilderError> for ExtendedBuilderError {
    fn from(error: BuilderError) -> Self {
        ExtendedBuilderError::Normal(error)
    }
}
impl From<ExtendedBuilderErrorKind> for ExtendedBuilderError {
    fn from(err: ExtendedBuilderErrorKind) -> Self {
        ExtendedBuilderError::from(Context::new(err))
    }
}

impl From<Context<ExtendedBuilderErrorKind>> for ExtendedBuilderError {
    fn from(err: Context<ExtendedBuilderErrorKind>) -> Self {
        ExtendedBuilderError::Extended(err)
    }
}
impl From<OtherBuilderErrorKind> for ExtendedBuilderError {
    fn from(err: OtherBuilderErrorKind) -> Self {
        ExtendedBuilderError::Normal(err.into())
    }
}

impl From<Context<OtherBuilderErrorKind>> for ExtendedBuilderError {
    fn from(err: Context<OtherBuilderErrorKind>) -> Self {
        ExtendedBuilderError::Normal(err.into())
    }
}

impl From<HeaderTypeError> for ExtendedBuilderError {
    fn from(err: HeaderTypeError) -> Self {
        ExtendedBuilderError::Normal(err.into())
    }
}

impl From<ComponentCreationError> for ExtendedBuilderError {
    fn from(err: ComponentCreationError) -> Self {
        ExtendedBuilderError::Normal(err.into())
    }
}

/// Error kinds associated with creating `MailSendData`
#[derive(Copy, Clone, Debug, Fail, PartialEq, Eq, Hash)]
pub enum MailSendDataErrorKind {
    /// No `From` was set.
    #[fail(display = "missing data for From field")]
    MissingFrom,

    /// No `To` was set.
    #[fail(display = "missing data for To field")]
    MissingTo,

    /// No `Subject` was set.
    #[fail(display = "missing data for Subject field")]
    MissingSubject,

    /// No `TemplateId` was given.
    #[fail(display = "missing template id")]
    MissingTemplateId,

    /// No `TemplateData` was given.
    #[fail(display = "missing template data")]
    MissingTemplateData,

    /// No `Sender` was given in an situation where it's needed.
    #[fail(display = "multiple mailboxes in from field but no sender field")]
    MultiFromButNoSender
}

/// Error returned when building `MailSendData` with the `MailSendDataBuilder` failed.
#[derive(Debug)]
pub struct MailSendDataError {
    inner: Context<MailSendDataErrorKind>,
}

impl Fail for MailSendDataError {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl Display for MailSendDataError {
    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(&self.inner, fter)
    }
}

impl From<MailSendDataErrorKind> for MailSendDataError {
    fn from(kind: MailSendDataErrorKind) -> Self {
        MailSendDataError::from(Context::new(kind))
    }
}

impl From<Context<MailSendDataErrorKind>> for MailSendDataError {
    fn from(inner: Context<MailSendDataErrorKind>) -> Self {
        MailSendDataError { inner }
    }
}