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
use serde::Deserialize;
use serde_json;

/// Kinds of errors that could happen at runtime.
#[derive(Debug)]
pub enum Error {
    /// An error occurred while parsing the received JSON
    ParsingError(serde_json::error::Error),

    /// An error occurred during a request to the APIs
    HTTPError(reqwest::Error),

    /// An error returned by the APIs
    LastFMError(LastFMErrorResponse),
}

/// Representation of all the LastFM APIs errors
#[derive(Debug)]
pub enum LastFMErrorResponse {
    InvalidService(LastFMError),
    InvalidMethod(LastFMError),
    AuthenticationFailed(LastFMError),
    InvalidFormat(LastFMError),
    InvalidParameter(LastFMError),
    InvalidResourceSpecified(LastFMError),
    OperationFailed(LastFMError),
    InvalidSessionKey(LastFMError),
    InvalidAPIKey(LastFMError),
    ServiceOffline(LastFMError),
    InvalidMethodSignatureSupplied(LastFMError),
    GenericError(LastFMError),
    SuspendedAPIKey(LastFMError),
    RateLimitExceeded(LastFMError),
}

/// A generic LastFM response when the request can't be accomplished.
#[derive(Deserialize, Debug)]
pub struct LastFMError {
    pub error: i32,
    pub message: String,
    pub links: Option<Vec<String>>,
}

impl From<LastFMError> for LastFMErrorResponse {
    fn from(lastm_error: LastFMError) -> LastFMErrorResponse {
        match lastm_error.error {
            2 => LastFMErrorResponse::InvalidService(lastm_error),
            3 => LastFMErrorResponse::InvalidMethod(lastm_error),
            4 => LastFMErrorResponse::AuthenticationFailed(lastm_error),
            5 => LastFMErrorResponse::InvalidFormat(lastm_error),
            6 => LastFMErrorResponse::InvalidParameter(lastm_error),
            7 => LastFMErrorResponse::InvalidResourceSpecified(lastm_error),
            8 => LastFMErrorResponse::OperationFailed(lastm_error),
            9 => LastFMErrorResponse::InvalidSessionKey(lastm_error),
            10 => LastFMErrorResponse::InvalidAPIKey(lastm_error),
            11 => LastFMErrorResponse::ServiceOffline(lastm_error),
            13 => LastFMErrorResponse::InvalidMethodSignatureSupplied(lastm_error),
            16 => LastFMErrorResponse::GenericError(lastm_error),
            26 => LastFMErrorResponse::SuspendedAPIKey(lastm_error),
            29 => LastFMErrorResponse::RateLimitExceeded(lastm_error),
            _ => LastFMErrorResponse::GenericError(lastm_error),
        }
    }
}