jrxFive / python-nomad

Client library Hashicorp Nomad
https://python-nomad.readthedocs.io/en/latest/
MIT License
139 stars 73 forks source link

Unhandled Exception due to Intersection of Request Exception Handling Logic + Exception's __str__ Method #107

Open jeffwecan opened 4 years ago

jeffwecan commented 4 years ago

https://github.com/jrxFive/python-nomad/pull/90 combined with https://github.com/jrxFive/python-nomad/pull/94 in v1.2.0 is leading to unintended behavior during certain classes of failing requests to a given Nomad API address. For example, in this (poorly formatted, sorry!) lambda's traceback:

Traceback (most recent call last):
[...]
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='10.55.3.155', port=4646): Read timed out. (read timeout=5)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
[...]
File "/var/task/nomad/api/node.py", line 182, in eligible_node
return self.request(id, "eligibility", json=payload, method="post").json()
File "/var/task/nomad/api/base.py", line 77, in request
response = self._request(
File "/var/task/nomad/api/base.py", line 164, in _request
raise nomad.api.exceptions.BaseNomadException(error)
nomad.api.exceptions.BaseNomadException: <exception str() failed>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/runtime/bootstrap", line 12, in <module>
main()
File "/var/runtime/bootstrap.py", line 473, in main
handle_event_request(lambda_runtime_client,
File "/var/runtime/bootstrap.py", line 145, in handle_event_request
xray_fault = make_xray_fault(etype.__name__, str(value), os.getcwd(), tb_tuples)
File "/var/task/nomad/api/exceptions.py", line 7, in __str__
return 'The {0} was raised with following response: {1}.'.format(self.__class__.__name__, self.nomad_resp.text)
AttributeError: 'ReadTimeout' object has no attribute 'text'

I'm not sure what is the best way to resolve the matter quite yet, but I'm happy to give it a shot! It seems like the crux of the matter, is that __str__ on BaseNomadException should not be making assumptions about what type of object is receives in its nomad_resp attribute or something similar.