Skip to main content

jira_cli/api/
mod.rs

1pub mod client;
2pub mod types;
3
4pub use client::JiraClient;
5pub use types::*;
6
7use std::fmt;
8
9#[derive(Debug)]
10pub enum ApiError {
11    /// Bad credentials or forbidden.
12    Auth(String),
13    /// Resource not found.
14    NotFound(String),
15    /// Invalid user input (bad key format, missing required value, etc.).
16    InvalidInput(String),
17    /// HTTP 429 rate limit.
18    RateLimit,
19    /// Non-2xx response from the Jira API.
20    Api { status: u16, message: String },
21    /// Network / TLS error.
22    Http(reqwest::Error),
23    /// Any other error.
24    Other(String),
25}
26
27impl fmt::Display for ApiError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            ApiError::Auth(msg) => write!(
31                f,
32                "Authentication failed: {msg}\nCheck JIRA_TOKEN or run `jira config show` to verify credentials."
33            ),
34            ApiError::NotFound(msg) => write!(f, "Not found: {msg}"),
35            ApiError::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
36            ApiError::RateLimit => write!(f, "Rate limited by Jira. Please wait and try again."),
37            ApiError::Api { status, message } => write!(f, "API error {status}: {message}"),
38            ApiError::Http(e) => write!(f, "HTTP error: {e}"),
39            ApiError::Other(msg) => write!(f, "{msg}"),
40        }
41    }
42}
43
44impl std::error::Error for ApiError {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            ApiError::Http(e) => Some(e),
48            _ => None,
49        }
50    }
51}
52
53impl From<reqwest::Error> for ApiError {
54    fn from(e: reqwest::Error) -> Self {
55        ApiError::Http(e)
56    }
57}