Browse Source

Handle status_code / status error in HTTP response

Checks for the existence of either 'status_code'
'status_int', or 'status' in the HTTP response object,
instantiating the HTTPException instance with the found
code, or raises a generic Exception if not found.

Test Plan:

PASS:
Tested code on system exhibiting the errors
referenced in the bug. The HTTP response is now
correctly caught and the correct HTTPException is
raised.

Closes-Bug: 1978499

Signed-off-by: Kyle MacLeod <kyle.macleod@windriver.com>
Change-Id: I53d1dd6583368de4f6713a838fe3304d362f1756
changes/90/845790/2
Kyle MacLeod 2 months ago
parent
commit
edd548d8c1
  1. 2
      sysinv/cgts-client/cgts-client/cgtsclient/common/http.py
  2. 11
      sysinv/cgts-client/cgts-client/cgtsclient/exc.py

2
sysinv/cgts-client/cgts-client/cgtsclient/common/http.py

@ -405,7 +405,7 @@ class HTTPClient(httplib2.Http):
error_json = self._extract_error_json(body_str)
raise exceptions.Forbidden(error_json.get('faultstring'))
elif 400 <= status_code < 600:
_logger.warn("Request returned failure status.") # pylint: disable=deprecated-method
_logger.warn("Request returned failure status: %s", status_code) # pylint: disable=deprecated-method
error_json = self._extract_error_json(body_str)
raise exceptions.from_response(
resp, error_json.get('faultstring'),

11
sysinv/cgts-client/cgts-client/cgtsclient/exc.py

@ -154,7 +154,16 @@ for obj_name in dir(sys.modules[__name__]):
def from_response(response, message=None, traceback=None,
method=None, url=None):
"""Return an instance of an HTTPException based on httplib response."""
cls = _code_map.get(response.status_code, HTTPException)
cls = None
if hasattr(response, 'status_code'):
cls = _code_map.get(response.status_code, HTTPException)
elif hasattr(response, 'status_int'):
cls = _code_map.get(response.status_int, HTTPException)
elif hasattr(response, 'status'):
cls = _code_map.get(response.status, HTTPException)
else:
# No status code: return a generic exception
return Exception("Unexpected error in response: %s" % message)
return cls(message)

Loading…
Cancel
Save