fetch_url, need to get the webpage content on urllib2.HTTPError exception.

Hi there,

I wrote modules to manage Dimension Data Cloud Platform. https://github.com/job-so/ansible.CaaS

These modules are working well, and already tested by different users for different use cases.

Before proposing a PR to ansible-modules-extras, i am cleaning the documentation and i am checking the community recommendations.
It includes the replacement of url2lib by fetch_url

I am facing the following issue : i need to handle the 400-Bad request HTTP error of an underlying API.
This API is providing detailed information about the error in the web page content.

[Quote from https://docs.python.org/2/library/urllib2.html]

*exception* `urllib2.``HTTPError`

Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

[/Quote]

So i need to access to e.read() method.

845: except urllib2.HTTPError, e:
+++: r = e # <=== or something smarter
846: info.update(dict(msg=str(e), status=e.code, **e.info()))

Do you think we can fix the code in this way ? I may do the fix/PR if needed.

Thank you

Olivier.

I wouldn’t have a problem with that approach. That sounds reasonable.

I tested that change with the integration tests, specifically for the uri and get_url modules, and everything was successful.

Thanks Matt,

As there is no other comment, here is the Pull Request :

https://github.com/ansible/ansible/pull/14915

Thank you

Olivier.