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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Error related stuff is here
//!
//! TODO: improve

use std::fmt;
use std::io;

use reqwest;
use serde::Deserialize;
use serde_json;
use url;

/// Result has Error as default value for Err value
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Represents library level error
#[derive(Deserialize)]
pub struct Error {
    detail: String,
    #[serde(skip_deserializing)]
    value: ErrValue,
}

impl Error {
    /// Error message
    pub fn detail(&self) -> String {
        self.detail.to_string()
    }

    /// Constructs error by its value
    pub fn with_value(val: ErrValue) -> Error {
        Error {
            detail: val.to_string(),
            value: val,
        }
    }

    /// Get the `ErrValue` enum for more specific error handling
    pub fn value(self) -> ErrValue {
        self.value
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Uploadcare: {}", self.value)
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Error: {}", self.value)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error {
            detail: err.to_string(),
            value: ErrValue::InputOutput(err),
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error {
            detail: err.to_string(),
            value: ErrValue::Reqwest(err),
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Error {
            detail: err.to_string(),
            value: ErrValue::SerdeJson(err),
        }
    }
}

impl From<url::ParseError> for Error {
    fn from(err: url::ParseError) -> Self {
        Error {
            detail: err.to_string(),
            value: ErrValue::ParseUrl(err),
        }
    }
}

/// Represents possible errors returned by the library
pub enum ErrValue {
    /// Endpoint parameters error
    BadRequest(String),
    /// Authorization errors
    Unauthorized(String),
    /// Forbidden error
    Forbidden(String),
    /// Not found error
    NotFound(String),
    /// Invalid version header `Accept` for the endpoint
    NotAcceptable(String),
    /// Payload too large
    PayloadTooLarge(String),
    /// Request was throttled
    TooManyRequests(i32),

    /// Errors returned from reqwest underlying lib
    Reqwest(reqwest::Error),
    /// Errors returned from io
    InputOutput(io::Error),
    /// JSON serialization/deserialization errors
    SerdeJson(serde_json::Error),
    /// Url parsing errors
    ParseUrl(url::ParseError),

    /// Other custom errors
    Other(String),
}

impl fmt::Display for ErrValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let prefix = "Uploadcare";

        match *self {
            ErrValue::BadRequest(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::Unauthorized(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::Forbidden(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::NotFound(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::NotAcceptable(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::PayloadTooLarge(ref msg) => write!(f, "{}: {}", prefix, msg),
            ErrValue::TooManyRequests(ref retry_after) => write!(
                f,
                "{}: too many requests, retry after {}",
                prefix, retry_after
            ),

            ErrValue::Reqwest(ref err) => write!(f, "{}: {}", prefix, err),
            ErrValue::InputOutput(ref err) => write!(f, "{}: {}", prefix, err),
            ErrValue::SerdeJson(ref err) => write!(f, "{}: {}", prefix, err),
            ErrValue::ParseUrl(ref err) => write!(f, "{}: {}", prefix, err),

            ErrValue::Other(ref msg) => write!(f, "{}: {}", prefix, msg),
        }
    }
}

impl Default for ErrValue {
    fn default() -> Self {
        ErrValue::Other("ErrValue".to_string())
    }
}