From 03b5a6f5d67ce7909a454f4ee1030b55205d533d Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 4 Dec 2019 08:06:35 +1000 Subject: [PATCH] galaxy - print HTTP reason on unknown error (#65273) (#65439) (cherry picked from commit 6586b7132c839b2f60582ff363a99c62156e2e50) --- changelogs/fragments/galaxy-error-reason.yaml | 2 ++ lib/ansible/galaxy/api.py | 6 +++--- test/units/galaxy/test_api.py | 10 ++++------ test/units/galaxy/test_collection_install.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/galaxy-error-reason.yaml diff --git a/changelogs/fragments/galaxy-error-reason.yaml b/changelogs/fragments/galaxy-error-reason.yaml new file mode 100644 index 00000000000..6a1bf0ab4c7 --- /dev/null +++ b/changelogs/fragments/galaxy-error-reason.yaml @@ -0,0 +1,2 @@ +bugfixes: +- ansible-galaxy - Return the HTTP code reason if no error msg was returned by the server - https://github.com/ansible/ansible/issues/64850 diff --git a/lib/ansible/galaxy/api.py b/lib/ansible/galaxy/api.py index ed9be32af3d..037e0666d21 100644 --- a/lib/ansible/galaxy/api.py +++ b/lib/ansible/galaxy/api.py @@ -110,7 +110,7 @@ class GalaxyError(AnsibleError): url_split = self.url.split('/') if 'v2' in url_split: - galaxy_msg = err_info.get('message', 'Unknown error returned by Galaxy server.') + galaxy_msg = err_info.get('message', http_error.reason) code = err_info.get('code', 'Unknown') full_error_msg = u"%s (HTTP Code: %d, Message: %s Code: %s)" % (message, self.http_code, galaxy_msg, code) elif 'v3' in url_split: @@ -120,7 +120,7 @@ class GalaxyError(AnsibleError): message_lines = [] for error in errors: - error_msg = error.get('detail') or error.get('title') or 'Unknown error returned by Galaxy server.' + error_msg = error.get('detail') or error.get('title') or http_error.reason error_code = error.get('code') or 'Unknown' message_line = u"(HTTP Code: %d, Message: %s Code: %s)" % (self.http_code, error_msg, error_code) message_lines.append(message_line) @@ -128,7 +128,7 @@ class GalaxyError(AnsibleError): full_error_msg = "%s %s" % (message, ', '.join(message_lines)) else: # v1 and unknown API endpoints - galaxy_msg = err_info.get('default', 'Unknown error returned by Galaxy server.') + galaxy_msg = err_info.get('default', http_error.reason) full_error_msg = u"%s (HTTP Code: %d, Message: %s)" % (message, self.http_code, galaxy_msg) self.message = to_native(full_error_msg) diff --git a/test/units/galaxy/test_api.py b/test/units/galaxy/test_api.py index 7464a7d7f4e..86cfaeaa6f4 100644 --- a/test/units/galaxy/test_api.py +++ b/test/units/galaxy/test_api.py @@ -214,8 +214,8 @@ def test_initialise_unknown(monkeypatch): api = GalaxyAPI(None, "test", "https://galaxy.ansible.com/api/", token=GalaxyToken(token='my_token')) - expected = "Error when finding available api versions from test (%s) (HTTP Code: 500, Message: Unknown " \ - "error returned by Galaxy server.)" % api.api_server + expected = "Error when finding available api versions from test (%s) (HTTP Code: 500, Message: msg)" \ + % api.api_server with pytest.raises(AnsibleError, match=re.escape(expected)): api.authenticate("github_token") @@ -292,15 +292,13 @@ def test_publish_collection(api_version, collection_url, collection_artifact, mo @pytest.mark.parametrize('api_version, collection_url, response, expected', [ ('v2', 'collections', {}, - 'Error when publishing collection to test (%s) (HTTP Code: 500, Message: Unknown error returned by Galaxy ' - 'server. Code: Unknown)'), + 'Error when publishing collection to test (%s) (HTTP Code: 500, Message: msg Code: Unknown)'), ('v2', 'collections', { 'message': u'Galaxy error messäge', 'code': 'GWE002', }, u'Error when publishing collection to test (%s) (HTTP Code: 500, Message: Galaxy error messäge Code: GWE002)'), ('v3', 'artifact/collections', {}, - 'Error when publishing collection to test (%s) (HTTP Code: 500, Message: Unknown error returned by Galaxy ' - 'server. Code: Unknown)'), + 'Error when publishing collection to test (%s) (HTTP Code: 500, Message: msg Code: Unknown)'), ('v3', 'artifact/collections', { 'errors': [ { diff --git a/test/units/galaxy/test_collection_install.py b/test/units/galaxy/test_collection_install.py index ae86d91db6a..eee00e53b31 100644 --- a/test/units/galaxy/test_collection_install.py +++ b/test/units/galaxy/test_collection_install.py @@ -403,7 +403,7 @@ def test_build_requirement_from_name_401_unauthorized(galaxy_server, monkeypatch monkeypatch.setattr(galaxy_server, 'get_collection_versions', mock_open) - expected = "error (HTTP Code: 401, Message: Unknown error returned by Galaxy server.)" + expected = "error (HTTP Code: 401, Message: msg)" with pytest.raises(api.GalaxyError, match=re.escape(expected)): collection.CollectionRequirement.from_name('namespace.collection', [galaxy_server, galaxy_server], '*', False)