errors.rs 3.69 KB
Newer Older
1
use diesel;
Jordan Petridis's avatar
Jordan Petridis committed
2
use diesel::r2d2;
3
use diesel_migrations::RunMigrationsError;
4
use http;
5
use hyper;
Jordan Petridis's avatar
Jordan Petridis committed
6
use native_tls;
7
use rss;
8
use url;
9
use xml;
10 11 12

use std::io;

13 14
use models::Source;

Jordan Petridis's avatar
Jordan Petridis committed
15 16 17 18 19 20
#[fail(
    display = "Request to {} returned {}. Context: {}",
    url,
    status_code,
    context
)]
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#[derive(Fail, Debug)]
pub struct HttpStatusError {
    url: String,
    status_code: hyper::StatusCode,
    context: String,
}

impl HttpStatusError {
    pub fn new(url: String, code: hyper::StatusCode, context: String) -> Self {
        HttpStatusError {
            url,
            status_code: code,
            context,
        }
    }
}

38
#[derive(Fail, Debug)]
39
pub enum DataError {
Jordan Petridis's avatar
Jordan Petridis committed
40
    #[fail(display = "SQL Query failed: {}", _0)]
41
    DieselResultError(#[cause] diesel::result::Error),
Jordan Petridis's avatar
Jordan Petridis committed
42
    #[fail(display = "Database Migration error: {}", _0)]
43
    DieselMigrationError(#[cause] RunMigrationsError),
Jordan Petridis's avatar
Jordan Petridis committed
44
    #[fail(display = "R2D2 error: {}", _0)]
45
    R2D2Error(#[cause] r2d2::Error),
Jordan Petridis's avatar
Jordan Petridis committed
46
    #[fail(display = "R2D2 Pool error: {}", _0)]
47
    R2D2PoolError(#[cause] r2d2::PoolError),
48 49
    #[fail(display = "Hyper Error: {}", _0)]
    HyperError(#[cause] hyper::Error),
50 51
    #[fail(display = "ToStr Error: {}", _0)]
    HttpToStr(#[cause] http::header::ToStrError),
52
    #[fail(display = "Failed to parse a url: {}", _0)]
53 54 55
    UrlError(#[cause] url::ParseError),
    #[fail(display = "TLS Error: {}", _0)]
    TLSError(#[cause] native_tls::Error),
56
    #[fail(display = "IO Error: {}", _0)]
Jordan Petridis's avatar
Jordan Petridis committed
57
    IOError(#[cause] io::Error),
58
    #[fail(display = "RSS Error: {}", _0)]
59
    RssError(#[cause] rss::Error),
60 61
    #[fail(display = "XML Reader Error: {}", _0)]
    XmlReaderError(#[cause] xml::reader::Error),
62
    #[fail(display = "Error: {}", _0)]
63
    Bail(String),
64 65
    #[fail(display = "{}", _0)]
    HttpStatusGeneral(HttpStatusError),
66 67
    #[fail(display = "Source redirects to a new url")]
    FeedRedirect(Source),
68 69
    #[fail(display = "Feed is up to date")]
    FeedNotModified(Source),
Jordan Petridis's avatar
Jordan Petridis committed
70 71 72 73
    #[fail(
        display = "Error occured while Parsing an Episode. Reason: {}",
        reason
    )]
74
    ParseEpisodeError { reason: String, parent_id: i32 },
75 76
    #[fail(display = "Episode was not changed and thus skipped.")]
    EpisodeNotChanged,
77 78
}

79
impl From<RunMigrationsError> for DataError {
80
    fn from(err: RunMigrationsError) -> Self {
81
        DataError::DieselMigrationError(err)
82 83 84
    }
}

85
impl From<diesel::result::Error> for DataError {
86
    fn from(err: diesel::result::Error) -> Self {
87
        DataError::DieselResultError(err)
88 89 90
    }
}

91
impl From<r2d2::Error> for DataError {
92
    fn from(err: r2d2::Error) -> Self {
93
        DataError::R2D2Error(err)
94 95 96
    }
}

97
impl From<r2d2::PoolError> for DataError {
98
    fn from(err: r2d2::PoolError) -> Self {
99
        DataError::R2D2PoolError(err)
100
    }
101
}
102 103 104 105 106 107 108

impl From<hyper::Error> for DataError {
    fn from(err: hyper::Error) -> Self {
        DataError::HyperError(err)
    }
}

109 110 111 112 113 114
impl From<http::header::ToStrError> for DataError {
    fn from(err: http::header::ToStrError) -> Self {
        DataError::HttpToStr(err)
    }
}

115 116 117 118 119 120 121 122 123 124 125
impl From<url::ParseError> for DataError {
    fn from(err: url::ParseError) -> Self {
        DataError::UrlError(err)
    }
}

impl From<native_tls::Error> for DataError {
    fn from(err: native_tls::Error) -> Self {
        DataError::TLSError(err)
    }
}
126 127 128 129 130 131

impl From<io::Error> for DataError {
    fn from(err: io::Error) -> Self {
        DataError::IOError(err)
    }
}
132

133 134 135 136 137 138
impl From<rss::Error> for DataError {
    fn from(err: rss::Error) -> Self {
        DataError::RssError(err)
    }
}

139 140 141 142 143 144
impl From<xml::reader::Error> for DataError {
    fn from(err: xml::reader::Error) -> Self {
        DataError::XmlReaderError(err)
    }
}

145 146
impl From<String> for DataError {
    fn from(err: String) -> Self {
147
        DataError::Bail(err)
148 149
    }
}