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
//! Client error types.

use std::error::Error;
use std::fmt;

use serde::Deserialize;
use serde_urlencoded;
use surf::http::StatusCode;
use surf::{self, http::url};

pub type ClientResult<T> = std::result::Result<T, ClientError>;

/// Type for the JSON error data returned on error from the HTTP API
/// The API seems a bit unreliable on the format of errors returned...
#[derive(Deserialize, Debug)]
pub struct ResponseError {
    pub error: String,
    pub error_description: String,
}

/// Type for the JSON error data returned on forbidden error from HTTP API
#[derive(Deserialize, Debug)]
pub struct ResponseCodeMessageError {
    pub error: CodeMessage,
}

#[derive(Deserialize, Debug)]
pub struct CodeMessage {
    pub code: u32,
    pub message: String,
}

/// Represents all error possibilities that could be returned by the client.
#[derive(Debug)]
pub enum ClientError {
    SurfError(surf::Error),
    SerdeJsonError(serde_json::error::Error),
    Unauthorized(ResponseError),
    Forbidden(ResponseCodeMessageError),
    ExpiredToken,
    IOError(std::io::Error),
    UrlParseError(url::ParseError),
    UrlEncodeError(serde_urlencoded::ser::Error),
    UnexpectedJsonStructure, // eg returned valid json but didn't fit model
    NotFound(ResponseCodeMessageError), // 404
    NotModified,             // 304
    Other(StatusCode, String), // ¯\_(ツ)_/¯
}

impl fmt::Display for ClientError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::ClientError::*;
        write!(
            f,
            "{}",
            match self {
                SerdeJsonError(e) => format!("Error deserializing json: {}", e),
                e => format!("{:?}", e),
            }
        )
    }
}

impl Error for ClientError {}

// TODO: extract surf errors and turn them into more useful ClientErrors
// TODO: maybe impl Error::cause to get the underlying surf or serde errors?

impl From<serde_json::error::Error> for ClientError {
    fn from(err: serde_json::error::Error) -> Self {
        ClientError::SerdeJsonError(err)
    }
}

impl From<std::io::Error> for ClientError {
    fn from(err: std::io::Error) -> Self {
        ClientError::IOError(err)
    }
}

impl From<url::ParseError> for ClientError {
    fn from(err: url::ParseError) -> Self {
        ClientError::UrlParseError(err)
    }
}

impl From<surf::Error> for ClientError {
    fn from(err: surf::Error) -> Self {
        ClientError::SurfError(err)
    }
}

impl From<serde_urlencoded::ser::Error> for ClientError {
    fn from(err: serde_urlencoded::ser::Error) -> Self {
        ClientError::UrlEncodeError(err)
    }
}

/// Represents possible errors building a `TagString`.
#[derive(Debug)]
pub enum TagStringError {
    ContainsComma,
}

impl fmt::Display for TagStringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                TagStringError::ContainsComma => "Contains comma (invalid character)",
            }
        )
    }
}

impl Error for TagStringError {}