use std::process::ExitCode;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("missing FMP API key; set FMP_API_KEY or pass --api-key")]
MissingApiKey,
#[error("invalid FMP base URL: {0}")]
InvalidBaseUrl(String),
#[error("missing required CLI argument: {0}")]
MissingArgument(&'static str),
#[error("FMP API request failed with HTTP {status}: {message}")]
Api {
status: u16,
message: String,
},
#[error("FMP API request was rate limited with HTTP {status}: {message}")]
RateLimited {
status: u16,
message: String,
},
#[error(
"empty result for symbol {symbol} from {endpoint}; try `fmp-agent search {search_query}` to verify the symbol, or rerun without --strict-empty to keep the raw FMP response"
)]
EmptyResult {
symbol: String,
search_query: String,
endpoint: &'static str,
},
#[error("HTTP request failed: {0}")]
Http(reqwest::Error),
#[error("failed to render JSON output: {0}")]
Json(#[from] serde_json::Error),
}
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Self {
Self::Http(error.without_url())
}
}
impl Error {
#[must_use]
pub const fn kind(&self) -> &'static str {
match self {
Self::MissingApiKey => "missing_api_key",
Self::InvalidBaseUrl(_) => "invalid_base_url",
Self::MissingArgument(_) => "missing_argument",
Self::Api { .. } => "api_error",
Self::RateLimited { .. } => "rate_limited",
Self::EmptyResult { .. } => "empty_result",
Self::Http(_) => "http_error",
Self::Json(_) => "json_error",
}
}
#[must_use]
pub fn exit_code(&self) -> ExitCode {
match self {
Self::MissingArgument(_) => ExitCode::from(2),
Self::MissingApiKey | Self::InvalidBaseUrl(_) => ExitCode::from(3),
Self::Http(_) => ExitCode::from(4),
Self::Api { .. } | Self::RateLimited { .. } => ExitCode::from(5),
Self::Json(_) => ExitCode::from(6),
Self::EmptyResult { .. } => ExitCode::from(7),
}
}
}