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
114
115
116
117
118
119
120
121
122
use crate::error::GraphError;
use crate::internal::GraphRsError;
use std::cell::BorrowMutError;
use std::io::ErrorKind;
use std::str::Utf8Error;
use std::sync::mpsc;
use std::{io, num, string};

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

    #[error("Parse error:\n{0:#?}")]
    Parse(#[from] num::ParseIntError),

    #[error("Parse string error:\n{0:#?}")]
    ParseString(#[from] string::ParseError),

    #[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("Serde yaml error:\n{0:#?}")]
    SerdeYamlError(#[from] serde_yaml::Error),

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

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

    #[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("Hyper http error:\n{0:#?}")]
    HyperError(#[from] hyper::Error),

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

    #[error("Hyper http error:\n{0:#?}")]
    HyperInvalidUri(#[from] hyper::http::uri::InvalidUri),

    #[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,
}

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() })
    }

    // pub fn from_response(r: &reqwest::blocking::Response) -> Option<GraphFailure> {
    //     GraphFailure::try_from(r).ok()
    // }

    // pub fn from_async_response(r: &reqwest::Response) -> Option<GraphFailure> {
    //     GraphFailure::try_from(r).ok()
    // }
}

impl Default for GraphFailure {
    fn default() -> Self {
        GraphFailure::GraphError(GraphError::default())
    }
}

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

// impl From<&reqwest::blocking::Response> for GraphFailure {
//     fn from(value: &reqwest::blocking::Response) -> Self {
//         GraphFailure::GraphError(GraphError::from(value))
//     }
// }

// impl From<&reqwest::Response> for GraphFailure {
//     fn from(value: &reqwest::Response) -> Self {
//         GraphFailure::GraphError(GraphError::from(value))
//     }
// }