1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
10pub enum Error {
11 #[error("HTTP request failed: {0}")]
13 Http(#[from] reqwest::Error),
14
15 #[error("Failed to parse JSON: {0}")]
17 Json(#[from] serde_json::Error),
18
19 #[error("API error: {message} (status: {status})")]
21 Api { status: u16, message: String },
22
23 #[error("API key not found. Please set FMP_API_KEY environment variable")]
25 MissingApiKey,
26
27 #[error("Invalid API key format")]
29 InvalidApiKey,
30
31 #[error("Invalid parameter: {0}")]
33 InvalidParameter(String),
34
35 #[error("Rate limit exceeded. Please try again later")]
37 RateLimitExceeded,
38
39 #[error("Resource not found: {0}")]
41 NotFound(String),
42
43 #[error("Failed to parse URL: {0}")]
45 UrlParse(#[from] url::ParseError),
46
47 #[error("{0}")]
49 Custom(String),
50}
51
52impl Error {
53 pub fn api(status: u16, message: impl Into<String>) -> Self {
55 Self::Api {
56 status,
57 message: message.into(),
58 }
59 }
60
61 pub fn custom(message: impl Into<String>) -> Self {
63 Self::Custom(message.into())
64 }
65
66 pub fn is_rate_limit(&self) -> bool {
68 matches!(self, Error::RateLimitExceeded)
69 || matches!(self, Error::Api { status, .. } if *status == 429)
70 }
71
72 pub fn is_not_found(&self) -> bool {
74 matches!(self, Error::NotFound(_))
75 || matches!(self, Error::Api { status, .. } if *status == 404)
76 }
77}