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
use std::fmt;

use reqwest_mock::header::InvalidHeaderValue;

/// Result type for `rs621`, using [`rs621::error::Error`].
///
/// [`rs621::error::Error`]: enum.Error.html
pub type Result<T> = ::std::result::Result<T, Error>;

/// Enum for `rs621` errors.
#[derive(Debug, PartialEq)]
pub enum Error {
    /// The given value for the some option is above the maximum value allowed in its context.
    /// E.g.: `order:score limit:350` is an invalid request because the maximum limit for ordered
    /// queries is 320.
    ///
    /// The first value is the name of the option, the second is the value that was given to it and
    /// the third is the biggest value allowed.
    AboveLimit(String, u64, u64),
    /// An HTTP error has occurred. The `u16` value is the HTTP error code.
    Http(u16),
    /// Serialization error. Contains a description of the error.
    Serial(String),
    /// Post JSON parsing error. The first value is the key of the invalid value, the second is its
    /// value.
    PostDeserialization(String, String),
    /// The request couldn't be send. Contains a description of the error.
    CannotSendRequest(String),
    /// The client couldn't be created. Contains a description of the error.
    CannotCreateClient(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::AboveLimit(option, value, max) => write!(
                f,
                "{}:{} is above the maximum value allowed in this context ({})",
                option, value, max
            ),
            Error::Http(code) => write!(f, "HTTP error: {}", code),
            Error::Serial(msg) => write!(f, "Serialization error: {}", msg),
            Error::PostDeserialization(k, v) => {
                write!(f, "Post JSON: invalid value for field \"{}\": {}", k, v)
            }
            Error::CannotSendRequest(msg) => write!(f, "Couldn't send request: {}", msg),
            Error::CannotCreateClient(msg) => write!(f, "Couldn't create client: {}", msg),
        }
    }
}

impl From<InvalidHeaderValue> for Error {
    fn from(e: InvalidHeaderValue) -> Error {
        Error::CannotCreateClient(format!("Invalid header value: {}", e))
    }
}