wallhaven_rs/client/error.rs
1use std::num::ParseIntError;
2
3use thiserror::Error;
4
5/// Possible errors inside the `wallhaven-rs` crate
6#[derive(Error, Debug)]
7pub enum Error {
8 /// Returned when an invalid api key is provided to `WallhavenClient::with_key`
9 #[error("Invalid api key.")]
10 InvalidApiKey(#[from] reqwest::header::InvalidHeaderValue),
11 /// Returned when a request fails to build the url from the provided parameters,
12 /// usually means the parameters are wrong
13 #[error(transparent)]
14 UrlParsingError(#[from] url::ParseError),
15 /// Returned when building `WallhavenClient`
16 ///
17 /// This error is thrown when building the `rewqest` client,
18 /// this means that its probably the library's fault if this happens (it probably will not happen though)
19 /// and you should open an issue!
20 #[error("Error building the client: {0}")]
21 BuildingClient(reqwest::Error),
22 /// There was some error while sending the request, you should match the underlying [`reqwest::Error`] further
23 #[error("Error sending a request: {0}")]
24 SendingRequest(reqwest::Error),
25 /// There was an error decoding the JSON, but the response was received successfully.
26 ///
27 /// Its an error either on wallhaven's side, if they sent a wrong json, or our side, if we wrote a bad model
28 #[error("Error deconding the request json: {0}")]
29 DecodingJson(reqwest::Error),
30 /// Some request error that isn't neither [`Error::SendingRequest`] nor [`Error::DecodingJson`]
31 ///
32 /// You can match further the underlying [`reqwest::Error`]
33 #[error("Unknown request error: {0}")]
34 UnknownRequestError(reqwest::Error),
35}
36
37/// Used only by `models::enums::color::Color` to parse `FromStr`
38#[derive(Error, Debug)]
39pub enum ColorParseError {
40 /// An invalid hex value was given
41 #[error("Invalid hex value. {}", .0)]
42 InvalidHex(#[from] ParseIntError),
43 /// An invalid color was given, not in the enum range
44 #[error("Invalid color value.")]
45 InvalidValue,
46}
47
48impl From<reqwest::Error> for Error {
49 fn from(value: reqwest::Error) -> Self {
50 if value.is_builder() {
51 Self::BuildingClient(value)
52 } else if value.is_decode() {
53 Self::DecodingJson(value)
54 } else if value.is_request() {
55 Self::SendingRequest(value)
56 } else {
57 Self::UnknownRequestError(value)
58 }
59 }
60}