Fix exception handling from response
The response class has a __bool__ method so checking 'if not response' has a different meaning than the code intended. Change-Id: I85f0f3642d752372f6aa33db296e26b2f81f5ee9
This commit is contained in:
parent
175f5faca4
commit
4d4c8a5568
|
@ -90,6 +90,16 @@ class MockRequestsResponse(object):
|
|||
def json(self):
|
||||
return jsonutils.loads(self.content)
|
||||
|
||||
def __bool__(self):
|
||||
"""Returns True if :attr:`status_code` is less than 400.
|
||||
|
||||
This attribute checks if the status code of the response is between
|
||||
400 and 600 to see if there was a client error or a server error. If
|
||||
the status code, is between 200 and 400, this will return True. This
|
||||
is **not** a check to see if the response code is ``200 OK``.
|
||||
"""
|
||||
return self.status_code < 400
|
||||
|
||||
|
||||
class MockRequestSessionApi(object):
|
||||
|
||||
|
|
|
@ -278,7 +278,7 @@ class NsxClientTestCase(NsxLibTestCase):
|
|||
super(NsxClientTestCase.MockHTTPProvider, self).__init__()
|
||||
if isinstance(session_response, list):
|
||||
self._session_responses = session_response
|
||||
elif session_response:
|
||||
elif session_response is not None:
|
||||
self._session_responses = [session_response]
|
||||
else:
|
||||
self._session_responses = None
|
||||
|
|
|
@ -52,7 +52,9 @@ def get_http_error_details(response):
|
|||
|
||||
|
||||
def init_http_exception_from_response(response):
|
||||
if not response:
|
||||
if response is None or response:
|
||||
# The response object has a __bool__ method that return True for
|
||||
# status code under 400. In that case there is no need for exception
|
||||
return None
|
||||
|
||||
error_details = get_http_error_details(response)
|
||||
|
|
Loading…
Reference in New Issue