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
use reqwest::StatusCode;
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub struct VndbApiError {
    pub status: Option<StatusCode>,
    pub message: String,
}

impl Error for VndbApiError {}

impl fmt::Display for VndbApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Error ({:#?}): {:#?}", self.status, self.message)
    }
}

impl VndbApiError {
    pub fn new(status: StatusCode, message: String) -> Self {
        Self {
            status: Some(status),
            message,
        }
    }
}

impl From<reqwest::Error> for VndbApiError {
    fn from(err: reqwest::Error) -> Self {
        let status = err.status();
        let message = format!("Reqwest error: {}", err);

        Self { status, message }
    }
}