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
use std::error::Error as StdError;
use std::fmt;

use log::error;

use crate::search::SearchParseError;

#[derive(Debug, Copy, Clone)]
pub enum ErrorKind {
    Database,
    Url,
    Body,
    NotFound,
    PermissionDenied,
    GoogleSignIn,
    GoogleUserNoEmail,
    GoogleUserNotFound,
    RegisteredTwiceForTest,
    RegistrationClosedForTest,
    OpenedTestNotRegistered,
    OpenedTestTwice,
    OpeningClosedForTest,
    SubmissionsClosedForTest,
    TestNotSubmitted,
    Network,
    Image,
    Font,
    Io,
    Unimplemented,
}

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    source: Option<Box<dyn StdError>>,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            ErrorKind::Database => write!(f, "Database error!"),
            ErrorKind::Url => write!(f, "Url parse error!"),
            ErrorKind::Body => write!(f, "Body parse error!"),
            ErrorKind::NotFound => write!(f, "Not found!"),
            ErrorKind::PermissionDenied => write!(f, "Permission denied!"),
            ErrorKind::GoogleSignIn => write!(f, "Failed to validate Id Token with Google"),
            ErrorKind::GoogleUserNoEmail => write!(f, "Google did not provide an email"),
            ErrorKind::GoogleUserNotFound => write!(
                f,
                "The email provided by Google did not match any users' emails"
            ),
            ErrorKind::Unimplemented => write!(f, "Method not implemented"),
            ErrorKind::RegisteredTwiceForTest => write!(f, "Registered twice for a test"),
            ErrorKind::RegistrationClosedForTest => {
                write!(f, "The test session is closed for registration")
            }
            ErrorKind::OpenedTestNotRegistered => write!(f, "Opened a test not registerd for"),
            ErrorKind::OpenedTestTwice => write!(f, "Opened a test twice"),
            ErrorKind::OpeningClosedForTest => write!(f, "The test session is closed"),
            ErrorKind::TestNotSubmitted => write!(f, "The test was not submitted"),
            ErrorKind::Network => write!(f, "There was a network problem"),
            ErrorKind::Image => write!(f, "There was an image problem"),
            ErrorKind::Io => write!(f, "There was an io problem"),
            ErrorKind::Font => write!(f, "There was a font problem"),
            ErrorKind::SubmissionsClosedForTest => {
                write!(f, "The test session is closed for submissions")
            }
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self.source {
            Some(ref e) => Some(e.as_ref()),
            None => None,
        }
    }
}

impl Error {
    pub fn new(kind: ErrorKind) -> Error {
        Error { kind, source: None }
    }

    pub fn with_source(kind: ErrorKind, source: Box<dyn StdError>) -> Error {
        Error {
            kind,
            source: Some(source),
        }
    }

    pub fn kind(&self) -> ErrorKind {
        return self.kind;
    }

    pub fn to_string_with_source(&self) -> String {
        if let Some(source) = &self.source {
            format!("{}:\n {}", self, source)
        } else {
            format!("{}", self)
        }
    }
}

impl From<diesel::result::Error> for Error {
    fn from(d: diesel::result::Error) -> Error {
        Error::with_source(ErrorKind::Database, Box::new(d))
    }
}

impl From<r2d2::Error> for Error {
    fn from(e: r2d2::Error) -> Error {
        Error::with_source(ErrorKind::Database, Box::new(e))
    }
}

impl From<serde_json::Error> for Error {
    fn from(s: serde_json::Error) -> Error {
        Error::with_source(ErrorKind::Body, Box::new(s))
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(s: std::num::ParseIntError) -> Error {
        Error::with_source(ErrorKind::Url, Box::new(s))
    }
}

impl From<std::str::ParseBoolError> for Error {
    fn from(s: std::str::ParseBoolError) -> Error {
        Error::with_source(ErrorKind::Url, Box::new(s))
    }
}

impl From<url::ParseError> for Error {
    fn from(s: url::ParseError) -> Error {
        Error::with_source(ErrorKind::Url, Box::new(s))
    }
}

impl From<image::ImageError> for Error {
    fn from(e: image::ImageError) -> Error {
        Error::with_source(ErrorKind::Image, Box::new(e))
    }
}

impl From<rusttype::Error> for Error {
    fn from(e: rusttype::Error) -> Error {
        Error::with_source(ErrorKind::Font, Box::new(e))
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Error {
        Error::with_source(ErrorKind::Io, Box::new(e))
    }
}

impl From<google_signin::Error> for Error {
    fn from(e: google_signin::Error) -> Error {
        Error::with_source(ErrorKind::PermissionDenied, Box::new(e))
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Error {
        Error::with_source(ErrorKind::Network, Box::new(e))
    }
}

impl From<SearchParseError> for Error {
    fn from(s: SearchParseError) -> Error {
        Error::with_source(ErrorKind::Url, Box::new(s))
    }
}

impl From<Error> for rouille::Response {
    fn from(e: Error) -> rouille::Response {
        error!("{:?} -> {:?}", e.kind(), e.source());

        match e.kind() {
            ErrorKind::Database => rouille::Response::text(e.to_string()).with_status_code(500),
            ErrorKind::Url => {
                rouille::Response::text(e.to_string_with_source()).with_status_code(400)
            }
            ErrorKind::Body => {
                rouille::Response::text(e.to_string_with_source()).with_status_code(400)
            }
            ErrorKind::NotFound => rouille::Response::text(e.to_string()).with_status_code(404),
            ErrorKind::GoogleSignIn => rouille::Response::text(e.to_string()).with_status_code(401),
            ErrorKind::GoogleUserNoEmail => {
                rouille::Response::text(e.to_string()).with_status_code(401)
            }
            ErrorKind::GoogleUserNotFound => {
                rouille::Response::text(e.to_string()).with_status_code(401)
            }
            ErrorKind::PermissionDenied => {
                rouille::Response::text(e.to_string()).with_status_code(401)
            }
            ErrorKind::RegisteredTwiceForTest => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::RegistrationClosedForTest => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::OpenedTestNotRegistered => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::OpenedTestTwice => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::OpeningClosedForTest => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::SubmissionsClosedForTest => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::TestNotSubmitted => {
                rouille::Response::text(e.to_string()).with_status_code(409)
            }
            ErrorKind::Network => {
                rouille::Response::text(e.to_string()).with_status_code(501)
            }
            ErrorKind::Image => {
                rouille::Response::text(e.to_string()).with_status_code(501)
            }
            ErrorKind::Font => {
                rouille::Response::text(e.to_string()).with_status_code(501)
            }
            ErrorKind::Io => {
                rouille::Response::text(e.to_string()).with_status_code(501)
            }
            ErrorKind::Unimplemented => {
                rouille::Response::text(e.to_string()).with_status_code(501)
            }
        }
    }
}