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
use crate::download::AsyncDownloadError;
use crate::internal::GraphRsError;
use crate::ErrorMessage;
use reqwest::header::HeaderMap;
use std::cell::BorrowMutError;
use std::io;
use std::io::ErrorKind;
use std::str::Utf8Error;
use std::sync::mpsc;

#[derive(Debug, thiserror::Error)]
#[allow(clippy::large_enum_variant)]
pub enum GraphFailure {
    #[error("IO error:\n{0:#?}")]
    Io(#[from] io::Error),

    #[error("Base 64 decode error:\n{0:#?}")]
    Utf8Error(#[from] Utf8Error),

    #[error("Request error:\n{0:#?}")]
    ReqwestError(#[from] reqwest::Error),

    #[error("Request error:\n{0:#?}")]
    ReqwestHeaderToStr(#[from] reqwest::header::ToStrError),

    #[error("Serde error:\n{0:#?}")]
    SerdeError(#[from] serde_json::error::Error),

    #[error("Base64 decode error:\n{0:#?}")]
    DecodeError(#[from] base64::DecodeError),

    #[error("Recv error:\n{0:#?}")]
    RecvError(#[from] mpsc::RecvError),

    #[error("Borrow Mut Error error:\n{0:#?}")]
    BorrowMutError(#[from] BorrowMutError),

    #[error("Url parse error:\n{0:#?}")]
    UrlParseError(#[from] url::ParseError),

    #[error("http::Error:\n{0:#?}")]
    HttpError(#[from] http::Error),

    #[error("Internal error:\n{0:#?}")]
    GraphRsError(#[from] GraphRsError),

    #[error("Handlebars render error:\n{0:#?}")]
    HandlebarsRenderError(#[from] handlebars::RenderError),

    #[error("Handlebars template render error:\n{0:?}")]
    HandlebarsTemplateRenderError(#[from] handlebars::TemplateRenderError),

    #[error("Crypto Error (Unknown)")]
    CryptoError,

    #[error("Async Download Error:\n{0:#?}")]
    AsyncDownloadError(#[from] AsyncDownloadError),

    #[error(
        "Error building or processing request prior to being sent:\n{0:#?}",
        error
    )]
    PreFlightError {
        url: Option<reqwest::Url>,
        headers: HeaderMap,
        error: Box<GraphFailure>,
    },

    #[error("{0:#?}", message)]
    Default {
        url: Option<reqwest::Url>,
        headers: Option<HeaderMap>,
        message: String,
    },

    #[error("{0:#?}")]
    ErrorMessage(#[from] ErrorMessage),
}

impl GraphFailure {
    pub fn error_kind(error_kind: io::ErrorKind, message: &str) -> Self {
        let e = io::Error::new(error_kind, message);
        GraphFailure::from(e)
    }

    pub fn internal(err: GraphRsError) -> GraphFailure {
        GraphFailure::GraphRsError(err)
    }

    pub fn not_found(msg: &str) -> GraphFailure {
        GraphFailure::error_kind(ErrorKind::NotFound, msg)
    }

    pub fn invalid(msg: &str) -> Self {
        GraphFailure::internal(GraphRsError::InvalidOrMissing { msg: msg.into() })
    }
}

impl Default for GraphFailure {
    fn default() -> Self {
        GraphFailure::Default {
            url: None,
            headers: None,
            message: "Unknown Error".into(),
        }
    }
}

impl From<ring::error::Unspecified> for GraphFailure {
    fn from(_: ring::error::Unspecified) -> Self {
        GraphFailure::CryptoError
    }
}