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
///! Library-specific errors, including types and implementations.
///!
use serde::{Deserialize, Serialize};

/// `RequestError` is raised when the Smartsheet API responds back with a
/// *non-* "OK" response.
///
/// More specifically, this error is raised when the status code of
/// a response is between 400 and 600, which indicates its either a client
/// error or a server error.
///
/// # Note
///
/// The `error` and `message` fields are mutually-exclusive; if we cannot
/// de-serialize `error`, the `message` will be populated instead with the
/// response data.
///
#[derive(Debug, Deserialize, Serialize)]
pub struct RequestError {
    pub status: u16,
    pub reason: String,
    pub message: Option<String>,
    pub error: Option<SmartsheetError>,
}

impl RequestError {
    /// Create a new `RequestError` object from a status code and reason.
    ///
    /// The `error` and `message` fields are mutually-exclusive, and so will
    /// both initially be unset.
    pub fn new(status: u16, reason: String) -> Self {
        Self {
            status,
            reason,
            message: None,
            error: None,
        }
    }
}
/// An error returned from the Smartsheet API, along with a custom error
/// code from the Smartsheet side.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#error-object
/// - https://smartsheet-platform.github.io/api-docs/#complete-error-code-list
///
#[derive(Debug, Deserialize, Serialize)]
pub struct SmartsheetError {
    pub message: String,
    #[serde(rename = "errorCode")]
    pub error_code: u16,
    #[serde(rename = "refId")]
    pub ref_id: Option<String>,
}

impl std::fmt::Display for RequestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for RequestError {}