Skip to content

Commit 59e2535

Browse files
Bugfix: added exception handling for HTTPError to provide response body when encountering HTTPError (#306)
1 parent 35511a1 commit 59e2535

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@
218218
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
219219
from ansible.module_utils.ansible_release import __version__ as ansible_version
220220
from ansible.errors import AnsibleError
221-
from ansible.module_utils._text import to_text
221+
from ansible.module_utils._text import to_text, to_native
222222
from ansible.module_utils.urls import open_url
223+
from ansible.module_utils.six.moves.urllib import error as urllib_error
223224
from ansible.module_utils.six.moves.urllib.parse import urlencode
224225
from ansible_collections.ansible.netcommon.plugins.module_utils.compat.ipaddress import (
225226
ip_interface,
@@ -254,12 +255,19 @@ def _fetch_information(self, url):
254255

255256
if need_to_fetch:
256257
self.display.v("Fetching: " + url)
257-
response = open_url(
258-
url,
259-
headers=self.headers,
260-
timeout=self.timeout,
261-
validate_certs=self.validate_certs,
262-
)
258+
try:
259+
response = open_url(
260+
url,
261+
headers=self.headers,
262+
timeout=self.timeout,
263+
validate_certs=self.validate_certs,
264+
)
265+
except urllib_error.HTTPError as e:
266+
"""This will return the response body when we encounter an error.
267+
This is to help determine what might be the issue when encountering an error.
268+
Please check issue #294 for more info.
269+
"""
270+
raise AnsibleError(to_native(e.fp.read()))
263271

264272
try:
265273
raw_data = to_text(response.read(), errors="surrogate_or_strict")

0 commit comments

Comments
 (0)