Skip to main content

flightradar24_api/
error.rs

1use std::fmt;
2
3/// Crate Error Types that may arise.
4#[derive(Debug)]
5pub enum FlightRadarError {
6    /// Errors returned by the HTTP client.
7    Http(reqwest::Error),
8    /// Errors that occur during parsing.
9    Parsing(String),
10    /// A general error with a message.
11    General(String),
12    /// Invalid Parameter Passed to API.
13    Parameter(String),
14}
15
16impl fmt::Display for FlightRadarError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            FlightRadarError::Http(err) => write!(f, "HTTP Error: {}", err),
20            FlightRadarError::Parsing(msg) => write!(f, "Parsing Error: {}", msg),
21            FlightRadarError::General(msg) => write!(f, "Error: {}", msg),
22            FlightRadarError::Parameter(msg) => write!(f, "Invalid Parameter: {}", msg),
23        }
24    }
25}
26
27impl std::error::Error for FlightRadarError {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        match self {
30            FlightRadarError::Http(err) => Some(err),
31            _ => None,
32        }
33    }
34}
35
36impl From<reqwest::Error> for FlightRadarError {
37    fn from(err: reqwest::Error) -> Self {
38        FlightRadarError::Http(err)
39    }
40}