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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Module containing all custom errors produced by this crate.
use std::fmt::{self, Display};
use std::io;

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

use internals::error::EncodingError;
use headers::error::{
    BuildInValidationError,
    HeaderTypeError, ComponentCreationError,
    HeaderValidationError
};
use ::IRI;
// errors from loading a Resource (which includes encoding it's body)
//                /  NotFound       | IRI (no Backtrace neede)     \ MailError::ResourceLoading
// ResourceError <   LoadingFailed  | chain Error                  /
//                \  EncodingFailed | EncodingError (Backtrace!)   > MailError::Encoding
//

/// Error caused by failing to load an `Resource`
///
#[derive(Debug, Fail)]
pub enum ResourceError {
    /// The loading on itself failed.
    #[fail(display = "{}", _0)]
    Loading(ResourceLoadingError),

    /// The encoding of the resource failed.
    ///
    /// Note: Resources are encoded as this allows shared
    /// resources to not be re-encoded every time they are
    /// used.
    #[fail(display = "{}", _0)]
    Encoding(EncodingError)
}

impl From<EncodingError> for ResourceError {
    fn from(err: EncodingError) -> Self {
        ResourceError::Encoding(err)
    }
}

impl From<ResourceLoadingError> for ResourceError {
    fn from(err: ResourceLoadingError) -> Self {
        ResourceError::Loading(err)
    }
}

/// Reasons why the loading of an `Resource` can fail.
#[derive(Copy, Clone, Debug, Fail, PartialEq, Eq, Hash)]
pub enum ResourceLoadingErrorKind {
    /// The resource wasn't found.
    #[fail(display = "resource not found")]
    NotFound,

    /// The act of loading it failed (e.g. because of an I/0-Error)
    #[fail(display = "loading failed")]
    LoadingFailed,

    #[fail(display = "automatically detecting the media type failed")]
    MediaTypeDetectionFailed
}

/// The loading of an Resource failed.
#[derive(Debug)]
pub struct ResourceLoadingError {
    inner: Context<ResourceLoadingErrorKind>,
    iri: Option<IRI>
}

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

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

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

impl ResourceLoadingError {

    /// The kind of error which caused the loading to fail.
    pub fn kind(&self) -> ResourceLoadingErrorKind {
        *self.inner.get_context()
    }

    /// The source IRI which was used when failing to load the Resource.
    pub fn source_iri(&self) -> Option<&IRI> {
        self.iri.as_ref()
    }

    /// Sets the source IRI if not already set and returns self.
    pub fn with_source_iri_or_else<F>(mut self, func: F) -> Self
        where F: FnOnce() -> Option<IRI>
    {
        if self.iri.is_none() {
            self.iri = func();
        }
        self
    }
}

impl From<ResourceLoadingErrorKind> for ResourceLoadingError {
    fn from(err: ResourceLoadingErrorKind) -> Self {
        ResourceLoadingError::from((None, err))
    }
}

impl From<Context<ResourceLoadingErrorKind>> for ResourceLoadingError {
    fn from(inner: Context<ResourceLoadingErrorKind>) -> Self {
        ResourceLoadingError::from((None, inner))
    }
}

impl From<(IRI, ResourceLoadingErrorKind)> for ResourceLoadingError {
    fn from((iri, error_kind): (IRI, ResourceLoadingErrorKind)) -> Self {
        ResourceLoadingError::from((Some(iri), error_kind))
    }
}

impl From<(IRI, Context<ResourceLoadingErrorKind>)> for ResourceLoadingError {
    fn from((iri, inner): (IRI, Context<ResourceLoadingErrorKind>)) -> Self {
        ResourceLoadingError::from((Some(iri), inner))
    }
}

impl From<(Option<IRI>, ResourceLoadingErrorKind)> for ResourceLoadingError {
    fn from((iri, error_kind): (Option<IRI>, ResourceLoadingErrorKind)) -> Self {
        ResourceLoadingError::from((iri, Context::new(error_kind)))
    }
}

impl From<(Option<IRI>, Context<ResourceLoadingErrorKind>)> for ResourceLoadingError {
    fn from((iri, inner): (Option<IRI>, Context<ResourceLoadingErrorKind>)) -> Self {
        ResourceLoadingError {
            inner, iri
        }
    }
}

impl From<io::Error> for ResourceLoadingError {
    fn from(err: io::Error) -> Self {
        err.context(ResourceLoadingErrorKind::LoadingFailed).into()
    }
}


#[derive(Debug, Fail)]
pub enum OtherValidationError {
    /// Non-multipart mail headers derive the Content-Type header from it's body `Resource`.
    ///
    /// This error is returned if a `Content-Type` header was given never the less.
    #[fail(display = "Content-Type header given for non multipart mail")]
    ContentTypeHeaderGiven,

    /// `Content-Transfer-Encoding` headers are always auto-generated
    /// and can not be manually set.
    #[fail(display = "Content-Transfer-Encoding header given")]
    ContentTransferEncodingHeaderGiven,

    /// A non "multipart" media type was given as content type for a multipart mail.
    #[fail(display = "found non multipart content type in multipart mail")]
    SingleMultipartMixup,

    /// Inserting a `Conent-Type` header into a singlepart body is not allowed.
    ///
    /// In single-part bodies the `Content-Type` header is always auto-generated
    /// based on the actual body.
    #[fail(display = "inserting Content-Type for singlepart body is not allowed")]
    InsertSinglepartContentTypeHeader,

    /// A multipart mail requires a `Content-Type` header to be given.
    #[fail(display = "multipart mail does not contain a content type header")]
    MissingContentTypeHeader,

    /// A mail (top level, not in multipart) requires a `From` header to be given.
    #[fail(display = "mail did not contain a From header")]
    NoFrom
}

impl From<OtherValidationError> for HeaderValidationError {
    fn from(oe: OtherValidationError) -> Self {
        let err: ::failure::Error = oe.into();
        HeaderValidationError::Custom(err)
    }
}

impl From<OtherValidationError> for MailError {
    fn from(oe: OtherValidationError) -> Self {
        let val_err = HeaderValidationError::from(oe);
        MailError::from(val_err)
    }
}

/// General Error combining most other error wrt. mail creation and encoding.
#[derive(Debug, Fail)]
pub enum MailError {
    /// Encoding the mail failed.
    #[fail(display = "{}", _0)]
    Encoding(EncodingError),

    /// Different implementations for the same header where mixed up.
    #[fail(display = "{}", _0)]
    Type(HeaderTypeError),

    /// Creating a mail header body (component) failed.
    #[fail(display = "{}", _0)]
    Component(ComponentCreationError),

    /// The mail has some invalid header or header combinations.
    ///
    /// E.g. it has a `From` header with multiple mailboxes but no
    /// `Sender` header (which is only required if `From` has more
    /// than one mailbox).
    #[fail(display = "{}", _0)]
    Validation(HeaderValidationError),

    /// Loading an resource failed.
    ///
    /// E.g. the file to attach or the image to embedded could not
    /// be found.
    #[fail(display = "{}", _0)]
    ResourceLoading(ResourceLoadingError)
}

impl From<BuildInValidationError> for MailError {
    fn from(err: BuildInValidationError) -> Self {
        MailError::Validation(err.into())
    }
}

impl From<HeaderTypeError> for MailError {
    fn from(err: HeaderTypeError) -> Self {
        MailError::Type(err)
    }
}

impl From<EncodingError> for MailError {
    fn from(err: EncodingError) -> Self {
        MailError::Encoding(err)
    }
}


impl From<HeaderValidationError> for MailError {
    fn from(err: HeaderValidationError) -> Self {
        MailError::Validation(err)
    }
}

impl From<ResourceLoadingError> for MailError {
    fn from(err: ResourceLoadingError) -> Self {
        MailError::ResourceLoading(err)
    }
}

impl From<ResourceError> for MailError {
    fn from(err: ResourceError) -> Self {
        match err {
            ResourceError::Loading(err) => MailError::ResourceLoading(err),
            ResourceError::Encoding(err) => MailError::Encoding(err)
        }
    }
}

impl From<ComponentCreationError> for MailError {
    fn from(err: ComponentCreationError) -> Self {
        MailError::Component(err)
    }
}


/// Error returned when trying to _unload_ and `Resource` and it fails.
#[derive(Copy, Clone, Debug, Fail)]
pub enum ResourceNotUnloadableError {
    /// The resource can not be unloaded because its in use.
    #[fail(display = "resource is in use, can't unload it")]
    InUse,
    /// The resource can not be unloaded because it doesn't has a source.
    ///
    /// Which means if we would unload it we could not reload it. Note
    /// that unloading is just for thinks like caching, it doesn't affect
    /// the deletion/dropping of `Resource` instances.
    #[fail(display = "resource has no source, can't unload it")]
    NoSource
}