graph-error 1.0.0

Graph Api error types and handling for the graph-rs-sdk crate
Documentation
use crate::download::AsyncDownloadError;
use crate::internal::GraphRsError;
use crate::{AuthExecutionError, AuthorizationFailure, ErrorMessage};
use reqwest::header::HeaderMap;
use std::cell::BorrowMutError;
use std::error::Error;
use std::io;
use std::io::ErrorKind;
use std::num::ParseIntError;
use std::str::Utf8Error;
use std::sync::mpsc;

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

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

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

    #[error("{0:#?}")]
    SerdeJson(#[from] serde_json::Error),

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

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

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

    #[error("{0:#?}")]
    UrlParse(#[from] url::ParseError),

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

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

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

    #[error("{0:?}")]
    HandlebarsTemplateRenderError(#[from] handlebars::TemplateRenderError),

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

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

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

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

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

    #[error("Temporary Graph API Error")]
    TemporaryError,

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

    #[error("message: {0:#?}, response: {1:#?}", message, response)]
    SilentTokenAuth {
        message: String,
        response: http::Response<Result<serde_json::Value, ErrorMessage>>,
    },

    #[error("{0:#?}")]
    JsonWebToken(#[from] jsonwebtoken::errors::Error),

    #[error("{0:#?}")]
    Other(#[from] Box<dyn Error + Send + Sync>),
}

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

impl From<AuthExecutionError> for GraphFailure {
    fn from(value: AuthExecutionError) -> Self {
        match value {
            AuthExecutionError::Authorization(authorization_failure) => match authorization_failure
            {
                AuthorizationFailure::RequiredValue { name, message } => {
                    GraphFailure::PreFlightError {
                        url: None,
                        headers: None,
                        error: None,
                        message: format!("name: {:#?}, message: {:#?}", name, message),
                    }
                }
                AuthorizationFailure::UrlParse(error) => GraphFailure::UrlParse(error),
                AuthorizationFailure::Uuid(error) => GraphFailure::PreFlightError {
                    url: None,
                    headers: None,
                    error: None,
                    message: format!(
                        "name: client_id, message: {:#?}, source: {:#?}",
                        "Client Id is not a valid Uuid",
                        error.to_string()
                    ),
                },
                AuthorizationFailure::Openssl(message) => GraphFailure::PreFlightError {
                    url: None,
                    headers: None,
                    error: None,
                    message,
                },
                AuthorizationFailure::SerdeJson(error) => GraphFailure::SerdeJson(error),
            },
            AuthExecutionError::Request(e) => GraphFailure::ReqwestError(e),
            AuthExecutionError::Http(e) => GraphFailure::HttpError(e),
            AuthExecutionError::SilentTokenAuth { message, response } => {
                GraphFailure::SilentTokenAuth { message, response }
            }
            AuthExecutionError::JsonWebToken(error) => GraphFailure::JsonWebToken(error),
            AuthExecutionError::Other(e) => GraphFailure::Other(e),
        }
    }
}