lotr_api/
error.rs

1//! Definition of the Error type for the crate.
2
3/// The error type for this crate.
4/// It is used to harmonize the error types of the dependencies and to add some custom errors.
5#[derive(Debug)]
6pub enum Error {
7    /// An error that occurred while serializing or deserializing JSON.
8    SerdeJson(serde_json::Error),
9    /// An error that occurred while making a request.
10    Reqwest(reqwest::Error),
11    InvalidSort,
12    InvalidFilter,
13    InvalidSecondaryItemType,
14    Other(String),
15}
16
17impl std::error::Error for Error {}
18
19impl std::fmt::Display for Error {
20    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
21        match self {
22            Self::SerdeJson(error) => write!(formatter, "SerdeJson error: {}", error),
23            Self::Reqwest(error) => write!(formatter, "Reqwest error: {}", error),
24            Self::InvalidSort => write!(formatter, "Invalid sort"),
25            Self::InvalidFilter => write!(formatter, "Invalid filter"),
26            Self::InvalidSecondaryItemType => write!(formatter, "Invalid secondary item type"),
27            Self::Other(message) => write!(formatter, "{}", message),
28        }
29    }
30}
31
32impl From<reqwest::Error> for Error {
33    fn from(error: reqwest::Error) -> Self {
34        Self::Reqwest(error)
35    }
36}
37
38impl From<serde_json::Error> for Error {
39    fn from(error: serde_json::Error) -> Self {
40        Self::SerdeJson(error)
41    }
42}