1pub mod client;
2pub mod types;
3
4pub use client::JiraClient;
5pub use types::*;
6
7use std::fmt;
8
9#[derive(Debug, Clone, PartialEq, Default)]
14pub enum AuthType {
15 #[default]
16 Basic,
17 Pat,
18}
19
20#[derive(Debug)]
21pub enum ApiError {
22 Auth(String),
24 NotFound(String),
26 InvalidInput(String),
28 ConfirmationRequired(String),
32 RateLimit,
34 Conflict(String),
38 Api { status: u16, message: String },
40 Http(reqwest::Error),
42 Other(String),
44}
45
46impl fmt::Display for ApiError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 ApiError::Auth(msg) => write!(
50 f,
51 "Authentication failed: {msg}\nCheck JIRA_TOKEN or run `jira config show` to verify credentials."
52 ),
53 ApiError::NotFound(msg) => write!(f, "Not found: {msg}"),
54 ApiError::InvalidInput(msg) => write!(f, "Invalid input: {msg}"),
55 ApiError::ConfirmationRequired(msg) => write!(f, "Confirmation required: {msg}"),
56 ApiError::RateLimit => write!(f, "Rate limited by Jira. Please wait and try again."),
57 ApiError::Conflict(msg) => write!(f, "Conflict: {msg}"),
58 ApiError::Api { status, message } => write!(f, "API error {status}: {message}"),
59 ApiError::Http(e) => write!(f, "HTTP error: {e}"),
60 ApiError::Other(msg) => write!(f, "{msg}"),
61 }
62 }
63}
64
65impl std::error::Error for ApiError {
66 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
67 match self {
68 ApiError::Http(e) => Some(e),
69 _ => None,
70 }
71 }
72}
73
74impl From<reqwest::Error> for ApiError {
75 fn from(e: reqwest::Error) -> Self {
76 ApiError::Http(e)
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83 use std::error::Error;
84
85 #[test]
86 fn auth_error_display_includes_check_guidance() {
87 let err = ApiError::Auth("invalid credentials".into());
88 let msg = err.to_string();
89 assert!(msg.contains("Authentication failed"));
90 assert!(msg.contains("invalid credentials"));
91 assert!(msg.contains("JIRA_TOKEN"), "should hint at how to fix auth");
92 }
93
94 #[test]
95 fn not_found_error_display_includes_message() {
96 let err = ApiError::NotFound("PROJ-999 not found".into());
97 let msg = err.to_string();
98 assert!(msg.contains("Not found"));
99 assert!(msg.contains("PROJ-999"));
100 }
101
102 #[test]
103 fn invalid_input_error_display_includes_message() {
104 let err = ApiError::InvalidInput("host is required".into());
105 let msg = err.to_string();
106 assert!(msg.contains("Invalid input"));
107 assert!(msg.contains("host is required"));
108 }
109
110 #[test]
111 fn rate_limit_error_display_is_actionable() {
112 let err = ApiError::RateLimit;
113 let msg = err.to_string();
114 assert!(msg.to_lowercase().contains("rate limit") || msg.contains("Rate limit"));
115 assert!(msg.contains("wait"), "should tell user to wait");
116 }
117
118 #[test]
119 fn api_error_display_includes_status_and_message() {
120 let err = ApiError::Api {
121 status: 422,
122 message: "Field 'foo' is required".into(),
123 };
124 let msg = err.to_string();
125 assert!(msg.contains("422"));
126 assert!(msg.contains("Field 'foo' is required"));
127 }
128
129 #[test]
130 fn other_error_display_is_message_verbatim() {
131 let err = ApiError::Other("something unexpected".into());
132 assert_eq!(err.to_string(), "something unexpected");
133 }
134
135 #[test]
136 fn http_error_source_is_the_underlying_reqwest_error() {
137 let rt = tokio::runtime::Runtime::new().unwrap();
138 let reqwest_err = rt.block_on(async {
139 reqwest::Client::new()
140 .get("http://127.0.0.1:1")
141 .send()
142 .await
143 .unwrap_err()
144 });
145 let api_err = ApiError::Http(reqwest_err);
146 assert!(
147 api_err.source().is_some(),
148 "Http variant must expose its source"
149 );
150 }
151
152 #[test]
153 fn non_http_variants_have_no_error_source() {
154 assert!(ApiError::Auth("x".into()).source().is_none());
155 assert!(ApiError::NotFound("x".into()).source().is_none());
156 assert!(ApiError::InvalidInput("x".into()).source().is_none());
157 assert!(
158 ApiError::ConfirmationRequired("x".into())
159 .source()
160 .is_none()
161 );
162 assert!(ApiError::RateLimit.source().is_none());
163 assert!(ApiError::Conflict("x".into()).source().is_none());
164 assert!(ApiError::Other("x".into()).source().is_none());
165 }
166
167 #[test]
168 fn conflict_error_display_includes_message() {
169 let err = ApiError::Conflict("issue was edited by someone else".into());
170 let msg = err.to_string();
171 assert!(msg.contains("Conflict"));
172 assert!(msg.contains("issue was edited by someone else"));
173 }
174
175 #[test]
178 fn confirmation_required_display_names_the_remedy() {
179 let err = ApiError::ConfirmationRequired("bulk-assign requires --yes".into());
180 let msg = err.to_string();
181 assert!(msg.contains("Confirmation required"));
182 assert!(msg.contains("--yes"));
183 }
184}