Skip to main content

twapi_v2/
error.rs

1use reqwest::StatusCode;
2use thiserror::Error;
3
4use crate::headers::Headers;
5
6#[derive(Error, Debug)]
7pub enum Error {
8    #[error("IO {0}")]
9    IO(#[from] std::io::Error),
10
11    #[error("Timeout")]
12    Timeout,
13
14    #[error("Upload {0}")]
15    Upload(String),
16
17    #[error("Other {0}")]
18    Other(String, Option<StatusCode>),
19
20    #[error("reqwest {0}")]
21    Reqwest(#[from] reqwest::Error),
22
23    #[error("serde json {0}")]
24    Json(#[from] serde_json::Error),
25
26    #[error("TwitterError {0:?}, {1:?}, {1:?}")]
27    Twitter(TwitterError, serde_json::Value, Box<Headers>), // https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
28}
29
30#[derive(Debug, Clone)]
31pub struct TwitterError {
32    pub status_code: StatusCode,
33    pub status: u64,
34    pub detail: String,
35    pub title: String,
36    pub r#type: String,
37}
38
39impl TwitterError {
40    pub fn new(source: &serde_json::Value, status_code: StatusCode) -> Self {
41        Self {
42            status_code,
43            status: source["status"].as_u64().unwrap_or_default(),
44            detail: source["detail"].as_str().unwrap_or_default().to_owned(),
45            title: source["title"].as_str().unwrap_or_default().to_owned(),
46            r#type: source["type"].as_str().unwrap_or_default().to_owned(),
47        }
48    }
49}