From 041824b98e3f4740a570175142380b0af511c646 Mon Sep 17 00:00:00 2001 From: Ken Dreyer Date: Mon, 27 Jul 2020 23:59:33 -0600 Subject: [PATCH] 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 Co-authored-by: Andrew Klychkov --- changelogs/fragments/311-jira-error-handling.yaml | 2 ++ plugins/modules/web_infrastructure/jira.py | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/311-jira-error-handling.yaml diff --git a/changelogs/fragments/311-jira-error-handling.yaml b/changelogs/fragments/311-jira-error-handling.yaml new file mode 100644 index 0000000000..11d73455fe --- /dev/null +++ b/changelogs/fragments/311-jira-error-handling.yaml @@ -0,0 +1,2 @@ +bugfixes: +- jira - improve error message handling (https://github.com/ansible-collections/community.general/pull/311). diff --git a/plugins/modules/web_infrastructure/jira.py b/plugins/modules/web_infrastructure/jira.py index e1eb6db1b9..9936439d52 100644 --- a/plugins/modules/web_infrastructure/jira.py +++ b/plugins/modules/web_infrastructure/jira.py @@ -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()