1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

jira: log error messages in "errors" key (#311)

Jira's API can return a empty "errorMessages" list, and the real error
is in the "errors" object. This happens, for example, if a user tries to
file a ticket in a project that does not exist (tested with Jira
v7.13.8)

Check both "errorMessages" and "errors", and report both values with
fail_json().

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
This commit is contained in:
Ken Dreyer 2020-07-27 23:59:33 -06:00 committed by GitHub
parent f5ed0689c1
commit 041824b98e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- jira - improve error message handling (https://github.com/ansible-collections/community.general/pull/311).

View file

@ -286,7 +286,7 @@ import sys
from ansible.module_utils.six.moves.urllib.request import pathname2url
from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils._text import to_text, to_bytes, to_native
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import fetch_url
@ -311,10 +311,17 @@ def request(url, user, passwd, timeout, data=None, method=None):
if info['status'] not in (200, 201, 204):
error = json.loads(info['body'])
if error:
module.fail_json(msg=error['errorMessages'])
msg = []
for key in ('errorMessages', 'errors'):
if error.get(key):
msg.append(error[key])
if msg:
module.fail_json(msg=to_native(', '.join(msg)))
else:
module.fail_json(msg=to_native(error))
else:
# Fallback print body, if it cant be decoded
module.fail_json(msg=info['body'])
module.fail_json(msg=to_native(info['body']))
body = response.read()