Skip to main content

rs621/
error.rs

1use reqwest::header::InvalidHeaderValue;
2
3use custom_error::custom_error;
4
5/// Result type for `rs621`, using [`rs621::error::Error`].
6///
7/// [`rs621::error::Error`]: enum.Error.html
8pub type Result<T> = std::result::Result<T, Error>;
9
10custom_error! { #[derive(PartialEq)] pub Error
11    AboveLimit{option: String, val: u64, max: u64} =
12        "{option}:{val} is above the maximum value allowed in this context ({max})",
13
14    Http{url: String, code: u16, reason: Option<String>} = @{
15        format!("HTTP error on URL \"{}\": {}{}", url, code, match reason {
16            Some(reason) => format!(": {}", reason),
17            // Give em a generic reason
18            None => match code {
19                200 => String::from(" OK: Request was successful"),
20                403 => String::from(" Forbidden: Access denied. May indicate that your request lacks a User-Agent header."),
21                404 => String::from(" Not Found"),
22                412 => String::from(" Precondition failed"),
23                420 => String::from(" Invalid Record: Record could not be saved"),
24                421 => String::from(" User Throttled: User is throttled, try again later"),
25                422 => String::from(" Locked: The resource is locked and cannot be modified"),
26                423 => String::from(" Already Exists: Resource already exists"),
27                424 => String::from(" Invalid Parameters: The given parameters were invalid"),
28                500 => String::from(" Internal Server Error: Some unknown error occurred on the server"),
29                502 => String::from(" Bad Gateway: A gateway server received an invalid response from the e621 servers"),
30                503 => String::from(" Service Unavailable: Server cannot currently handle the request or you have exceeded the request rate limit. Try again later or decrease your rate of requests."),
31                520 => String::from(" Unknown Error: Unexpected server response which violates protocol"),
32                522 => String::from(" Origin Connection Time-out: CloudFlare's attempt to connect to the e621 servers timed out"),
33                524 => String::from(" Origin Connection Time-out: A connection was established between CloudFlare and the e621 servers, but it timed out before an HTTP response was received"),
34                525 => String::from(" SSL Handshake Failed: The SSL handshake between CloudFlare and the e621 servers failed"),
35                _ => String::new(),
36            },
37        })
38    },
39
40    Serial{desc: String} = "Serialization error: {desc}",
41
42    Deserialization{desc: String} = "Deserialization error: {desc}",
43
44    CannotSendRequest{desc: String} = "Couldn't send request: {desc}",
45
46    CannotCreateClient{desc: String} = "Couldn't create client: {desc}",
47
48    InvalidHeaderValue{desc: String} = "Invalid header value",
49
50    PostNotFound{id: u64} = "Post #{id} not found",
51}
52
53impl From<InvalidHeaderValue> for Error {
54    fn from(e: InvalidHeaderValue) -> Error {
55        Error::InvalidHeaderValue {
56            desc: format!("Invalid header value: {}", e),
57        }
58    }
59}
60
61impl From<serde_json::error::Error> for Error {
62    fn from(e: serde_json::error::Error) -> Error {
63        Error::Deserialization {
64            desc: format!("{:#?}", e),
65        }
66    }
67}