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
use self::ErrorKind::*;
use crate::resource::ErrorObject;
use reqwest::StatusCode;
use std::fmt;
use std::result::Result as StdResult;

/// An alias to `Result` with `Err` of `onedrive_api::Error`.
pub type Result<T> = StdResult<T, Error>;

/// The error may occur when processing requests.
#[derive(Debug)]
pub struct Error {
    // Make the size of `Error` smaller.
    inner: Box<InnerError>,
}

#[derive(Debug)]
struct InnerError {
    kind: ErrorKind,
}

#[derive(Debug)]
enum ErrorKind {
    RequestError {
        source: reqwest::Error,
        response: Option<ErrorObject>,
    },
    UnexpectedResponse {
        reason: &'static str,
    },
}

impl Error {
    pub(crate) fn unexpected_response(reason: &'static str) -> Self {
        Self {
            inner: Box::new(InnerError {
                kind: ErrorKind::UnexpectedResponse { reason },
            }),
        }
    }

    pub(crate) fn from_response(source: reqwest::Error, response: Option<ErrorObject>) -> Self {
        Self {
            inner: Box::new(InnerError {
                kind: ErrorKind::RequestError { source, response },
            }),
        }
    }

    /// Check whether the error may be recovered by retrying.
    pub fn should_retry(&self) -> bool {
        match &self.inner.kind {
            RequestError { source, .. } => !source.is_client_error() && !source.is_serialization(),
            _ => false,
        }
    }

    /// Get the url related to the error.
    pub fn url(&self) -> Option<&reqwest::Url> {
        match &self.inner.kind {
            RequestError { source, .. } => source.url(),
            _ => None,
        }
    }

    /// Get the error response from API if caused by error status code.
    pub fn error_response(&self) -> Option<&ErrorObject> {
        match &self.inner.kind {
            RequestError { response, .. } => response.as_ref(),
            _ => None,
        }
    }

    /// Get the HTTP status code if caused by error status code.
    pub fn status_code(&self) -> Option<StatusCode> {
        match &self.inner.kind {
            RequestError { source, .. } => source.status(),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.inner.kind {
            RequestError { source, .. } => write!(f, "{}", source),
            UnexpectedResponse { reason } => write!(f, "{}", reason),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.inner.kind {
            RequestError { source, .. } => Some(source),
            _ => None,
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(source: reqwest::Error) -> Self {
        Self::from_response(source, None)
    }
}