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
use std::{error, fmt};
use reqwest::StatusCode;
use crate::ErrorData;
#[derive(Debug)]
pub enum Error {
ReqwestError(reqwest::Error),
SimpleNanoError(StatusCode, String),
NanoErrors(Vec<ErrorData>)
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ReqwestError(err) => write!(f, "Reqwest Error: {}", err),
Error::SimpleNanoError(code, message) => write!(f, "NanoWrimo API Error: {} (status code {})", message, code.as_u16()),
Error::NanoErrors(errs) => {
errs.iter().map(|err| {
write!(f, "{} ({}): {} (status code {})", err.title, err.code, err.detail, err.status)
}).collect::<Result<_, _>>()
},
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::ReqwestError(err) => Some(err),
Error::SimpleNanoError(..) => None,
Error::NanoErrors(..) => None,
}
}
}
impl From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Error {
Error::ReqwestError(err)
}
}