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

#[derive(Debug)]
pub enum IpApiError {
    PrivateRange,
    ReservedRange,
    InvalidQuery,
    Quota,
    OtherError(String)
}

impl Error for IpApiError {
    fn description(&self) -> &str {
        match *self {
            IpApiError::PrivateRange => "The IP address is part of a private range",
            IpApiError::ReservedRange => "The IP address is part of a reserved range",
            IpApiError::InvalidQuery => "Invalid IP address or domain name",
            IpApiError::Quota => "Quota exceeded, go unban yourself",
            IpApiError::OtherError(_) => "Some other error has occurred"
        }
    }
}

impl fmt::Display for IpApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Error: {}", self.description())
    }
}