HTTPError can also function as a non-exceptional file-like return value (#14915)

* HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns)

* HTTPError - adding response to info dictionnary

* HTTPError - adding response to info dictionnary

* HTTPError - adding body response to info dictionnary
This commit is contained in:
Olivier GROSJEANNE 2016-04-25 19:21:45 +02:00 committed by James Cammarata
parent f296d74329
commit 4647e8b74e

View file

@ -897,7 +897,11 @@ def fetch_url(module, url, data=None, headers=None, method=None,
except (ConnectionError, ValueError), e:
module.fail_json(msg=str(e))
except urllib2.HTTPError, e:
info.update(dict(msg=str(e), status=e.code, **e.info()))
try:
body = e.read()
except AttributeError:
body = ''
info.update(dict(msg=str(e), status=e.code, body=body, **e.info()))
except urllib2.URLError, e:
code = int(getattr(e, 'code', -1))
info.update(dict(msg="Request failed: %s" % str(e), status=code))