Skip to content

Commit e3f6caf

Browse files
committed
Report Jira errors returned by the REST API
1 parent 64687f4 commit e3f6caf

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/access.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
// * https://docs.atlassian.com/jira-software/REST/latest/
2020

2121
use crate::errors::JiraQueryError;
22-
use crate::issue_model::{Issue, JqlResults};
22+
use crate::issue_model::{Issue, JiraErrorMessages, JqlResults};
2323

2424
// The prefix of every subsequent REST request.
2525
// This string comes directly after the host in the URL.
@@ -170,11 +170,19 @@ impl JiraInstance {
170170
let url = self.path(&Method::Key(key), 0);
171171

172172
// Gets an issue by ID and deserializes the JSON to data variable
173-
let issue = self.authenticated_get(&url).await?.json::<Issue>().await?;
173+
let response = self.authenticated_get(&url).await?;
174174

175-
log::debug!("{:#?}", issue);
175+
if response.status().is_success() {
176+
let issue = response.json::<Issue>().await?;
176177

177-
Ok(issue)
178+
log::debug!("{:#?}", issue);
179+
180+
Ok(issue)
181+
} else {
182+
Err(JiraQueryError::JiraError(
183+
response.json::<JiraErrorMessages>().await?.error_messages,
184+
))
185+
}
178186
}
179187

180188
/// Access several issues by their keys.
@@ -251,15 +259,19 @@ impl JiraInstance {
251259
) -> Result<Vec<Issue>, JiraQueryError> {
252260
let url = self.path(method, start_at);
253261

254-
let results = self
255-
.authenticated_get(&url)
256-
.await?
257-
.json::<JqlResults>()
258-
.await?;
262+
let response = self.authenticated_get(&url).await?;
259263

260-
log::debug!("{:#?}", results);
264+
if response.status().is_success() {
265+
let results = response.json::<JqlResults>().await?;
261266

262-
Ok(results.issues)
267+
log::debug!("{:#?}", results);
268+
269+
Ok(results.issues)
270+
} else {
271+
Err(JiraQueryError::JiraError(
272+
response.json::<JiraErrorMessages>().await?.error_messages,
273+
))
274+
}
263275
}
264276

265277
/// Access issues using a free-form JQL search.

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ pub enum JiraQueryError {
2525
NoIssues,
2626
#[error("Error in accessing the Jira REST API.")]
2727
Request(#[from] reqwest::Error),
28+
#[error("Error in accessing the Jira REST API: {}", .0.join(", "))]
29+
JiraError(Vec<String>),
2830
}

src/issue_model.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,10 @@ pub struct Security {
432432
#[serde(flatten)]
433433
pub extra: Value,
434434
}
435+
436+
/// Error messages returned by Jira REST API.
437+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
438+
pub struct JiraErrorMessages {
439+
#[serde(rename = "errorMessages")]
440+
pub error_messages : Vec<String>
441+
}

0 commit comments

Comments
 (0)