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
use reqwest::StatusCode;
use thiserror::Error;

use crate::rate_limit::RateLimit;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Timeout")]
    Timeout,

    #[error("Other {0}")]
    Other(String, Option<StatusCode>),

    #[error("reqwest {0}")]
    Reqwest(#[from] reqwest::Error),

    #[error("serde json {0}")]
    Json(#[from] serde_json::Error),

    #[error("StatusError {0:?}")]
    Twitter(TwitterError, serde_json::Value, Option<RateLimit>),
}

#[derive(Debug, Clone)]
pub struct TwitterError {
    pub status_code: StatusCode,
    pub status: u64,
    pub detail: String,
    pub title: String,
    pub r#type: String,
}

impl TwitterError {
    pub fn new(source: &serde_json::Value, status_code: StatusCode) -> Self {
        Self {
            status_code,
            status: source["status"].as_u64().unwrap_or_default(),
            detail: source["detail"].as_str().unwrap_or_default().to_owned(),
            title: source["title"].as_str().unwrap_or_default().to_owned(),
            r#type: source["type"].as_str().unwrap_or_default().to_owned(),
        }
    }
}