smartsheet_rs/models/error.rs
1//! Library-specific errors, including types and implementations.
2//!
3use serde::{Deserialize, Serialize};
4
5/// `RequestError` is raised when the Smartsheet API responds back with a
6/// *non-* "OK" response.
7///
8/// More specifically, this error is raised when the status code of
9/// a response is between 400 and 600, which indicates its either a client
10/// error or a server error.
11///
12/// # Note
13///
14/// The `error` and `message` fields are mutually-exclusive; if we cannot
15/// de-serialize `error`, the `message` will be populated instead with the
16/// response data.
17///
18#[derive(Debug, Deserialize, Serialize)]
19pub struct RequestError {
20 pub status: u16,
21 pub reason: String,
22 pub message: Option<String>,
23 pub error: Option<SmartsheetError>,
24}
25
26impl RequestError {
27 /// Create a new `RequestError` object from a status code and reason.
28 ///
29 /// The `error` and `message` fields are mutually-exclusive, and so will
30 /// both initially be unset.
31 pub fn new(status: u16, reason: String) -> Self {
32 Self {
33 status,
34 reason,
35 message: None,
36 error: None,
37 }
38 }
39}
40/// An error returned from the Smartsheet API, along with a custom error
41/// code from the Smartsheet side.
42///
43/// # Docs
44/// - https://smartsheet-platform.github.io/api-docs/#error-object
45/// - https://smartsheet-platform.github.io/api-docs/#complete-error-code-list
46///
47#[derive(Debug, Deserialize, Serialize)]
48#[serde(rename_all = "camelCase")]
49pub struct SmartsheetError {
50 pub message: String,
51 pub error_code: u16,
52 pub ref_id: Option<String>,
53}
54
55impl std::fmt::Display for RequestError {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{:?}", self)
58 }
59}
60
61impl std::error::Error for RequestError {}