New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exception thrown with get_url() when returned HTTP response 422 #17108
Comments
@ldgeorge85 The urllib2.urlopen() will return the response (result) only if the response code is between 200 to 300. You can check it here https://github.com/python-git/python/blob/master/Lib/urllib2.py
|
@vikas-t, thanks for your comment - it quickly got to the crux of the problem - but then shouldn't open_url() handle the exception from urllib2 and return to the caller? I understand what you said, but it feels like there's more to say. I'd expect you to add either:
I suppose one might suggest that the caller should handle the exception from urllib2, but that doesn't make sense since presumably the point of ansible.module_utils.urls is to encapsulate what is underneath it. I'm on Ansible 2.4 and I just ran into this frustrating problem. I didn't have it with requests (when my code was in a standalone script), and I thought I was doing a good thing by going to open_url(). Like requests, curl is similarly friendly:
How do I get this same functionality with open_url()? If I do the exact same request as I did in curl or requests that produced the above example result, the wheels come off. |
Should you want automatic exception handling, please utilize If you have further questions please stop by IRC or the mailing list:
|
BTW, the example at the top of the fetch_url() def says:
Shouldn't that be response.read() ? |
ISSUE TYPE
COMPONENT NAME
module_utils/url.py
open_url()
ANSIBLE VERSION
CONFIGURATION
Only changes in ansible.cfg were to:
forks = 11
host_key_checking = False
Other than that, all is completely default.
OS / ENVIRONMENT
CentOS Linux release 7.2.1511 (Core)
Ansible installed using epel repo.
SUMMARY
I am trying to develop a custom module. Part of the module uses the open_url() function from ansible. I was initially using requests module, but I wanted to try to make my ansible module dependent on only ansible itself. Anyway, I have ran into a problem with error handling using open_url(). It seems to go back to the urllib2 code. Basically, when I get back a HTTP response code of 422, it is giving an exception and dying. I have code in my module to catch the response codes and try to fail gracefully, but it does not even seem to reach that point, at least from what the trace shows. When I was using the requests module, this worked fine, the module itself just passed the response back to my code to handle what to do.
I am not sure if there is something I can pass it that would change this or if this is truly a bug. I have tried looking at dev versions of this, but there seems to be a lot of changes, though nothing that mentions this.
Just to note, I am getting no errors and it is passing things back fine when the HTTP response is 201(the expected return).
STEPS TO REPRODUCE
I am using this is a custom ansible module I am building. Here are the lines that call that function and then try to catch the HTTP responses.
EXPECTED RESULTS
I expect it to fail gracefully with ansible, dumping the failure meta data, like:
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "meta": {"errors": {"base": [""], "initial_root_password": ["should include letters and digits"], "primary_network_group_id": ["could not be found."]}}, "msg": "Error creating VM"}
ACTUAL RESULTS
Instead, I am getting it dying, with a trace. It does show that there was a 422, but it does not fail gracefully at all and does not return any of the meta data from the failure.
The text was updated successfully, but these errors were encountered: