Skip to content

Commit 920b1b0

Browse files
authored
lists and dicts can be handled in raise_for_status (#1234)
* lists and dicts can be handled in raise_for_status * Fixed some formatting --------- Co-authored-by: chri.wapp@gmail.com <christian.wappler@de.bosch.com>
1 parent fa7415e commit 920b1b0

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

atlassian/rest_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,13 @@ def raise_for_status(self, response):
476476
if self.url == "https://api.atlassian.com":
477477
error_msg = "\n".join([k + ": " + v for k, v in j.items()])
478478
else:
479-
error_msg = "\n".join(
480-
j.get("errorMessages", list())
481-
+ [
482-
k.get("message", "") if isinstance(k, dict) else v
483-
for k, v in j.get("errors", dict()).items()
484-
]
485-
)
479+
error_msg_list = j.get("errorMessages", list())
480+
errors = j.get("errors", dict())
481+
if isinstance(errors, dict):
482+
error_msg_list.append(errors.get("message", ""))
483+
elif isinstance(errors, list):
484+
error_msg_list.extend([v.get("message", "") if isinstance(v, dict) else v for v in errors])
485+
error_msg = "\n".join(error_msg_list)
486486
except Exception as e:
487487
log.error(e)
488488
response.raise_for_status()

tests/mockup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def request_mockup(*args, **kwargs):
3939
response.status_code = data.pop("status_code")
4040
else:
4141
response.status_code = 200
42+
if "headers" in data:
43+
response.headers = data.pop("headers")
4244

4345
# Extend the links with the server
4446
for item in [None, "owner", "project", "workspace"]:

tests/responses/jira/rest/api/2/issue/POST

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,14 @@ responses['{"fields": {"issuetype": "foo", "summary": "summary", "project": "pro
33
"errorMessages": [],
44
"errors": {"labels": "Field 'labels' cannot be set. It is not on the appropriate screen, or unknown."},
55
}
6+
responses['{"fields": {"issuetype": "fail", "summary": "authentication", "project": "project"}}'] = {
7+
"status_code": 401,
8+
"headers": {"Content-Type": "application/json;charset=UTF-8"},
9+
"errors": [
10+
{
11+
"context": None,
12+
"message": "Authentication failed. Please check your credentials and try again.",
13+
"exceptionName": "com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException",
14+
}
15+
],
16+
}

tests/test_jira.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ def test_post_issue_with_invalid_request(self):
4949
"""Post an issue but receive a 400 error response"""
5050
with self.assertRaises(HTTPError):
5151
self.jira.create_issue(fields={"issuetype": "foo", "summary": "summary", "project": "project"})
52+
53+
def test_post_issue_expect_failed_authentication(self):
54+
"""Post an issue but receive a 401 error response"""
55+
with self.assertRaises(HTTPError):
56+
self.jira.create_issue(fields={"issuetype": "fail", "summary": "authentication", "project": "project"})

0 commit comments

Comments
 (0)