Use related error codes to decide on the exception

In case the error code has no specific exception, try the related
error codes as well.

Cherry-picked from: I2054e56705545f25ff2cad359e4091dbf04bb17b

Change-Id: I9f80e3a238c941b6ff7b7d2277119c2ffb7026b8
This commit is contained in:
asarfaty 2020-10-12 08:02:54 +02:00
parent 0db5522098
commit 1a047bd481
2 changed files with 21 additions and 4 deletions

View File

@ -314,6 +314,14 @@ class NsxV3RESTClientTestCase(nsxlib_testcase.NsxClientTestCase):
exc = client.http_error_to_exception(500, 607)
self.assertEqual(exc, nsxlib_exc.APITransactionAborted)
exc = client.http_error_to_exception(
requests.codes.INTERNAL_SERVER_ERROR, 98, [777])
self.assertEqual(exc, nsxlib_exc.CannotConnectToServer)
exc = client.http_error_to_exception(requests.codes.BAD_REQUEST,
500157, [777, 500045])
self.assertEqual(exc, nsxlib_exc.NsxPendingDelete)
class NsxV3JSONClientTestCase(nsxlib_testcase.NsxClientTestCase):

View File

@ -29,7 +29,7 @@ LOG = log.getLogger(__name__)
NULL_CURSOR_PREFIX = '0000'
def http_error_to_exception(status_code, error_code):
def http_error_to_exception(status_code, error_code, related_error_codes=None):
errors = {
requests.codes.NOT_FOUND:
{'202': exceptions.BackendResourceNotFound,
@ -62,7 +62,12 @@ def http_error_to_exception(status_code, error_code):
# choose based on error code
if error_code and str(error_code) in errors[status_code]:
return errors[status_code][str(error_code)]
elif 'default' in errors[status_code]:
# try the related errors
if related_error_codes:
for err in related_error_codes:
if err and str(err) in errors[status_code]:
return errors[status_code][str(err)]
if 'default' in errors[status_code]:
return errors[status_code]['default']
else:
return errors[status_code]
@ -154,7 +159,9 @@ class RESTClient(object):
def _raise_error(self, status_code, operation, result_msg,
error_code=None, related_error_codes=None):
error = http_error_to_exception(status_code, error_code)
error = http_error_to_exception(
status_code, error_code,
related_error_codes=related_error_codes)
raise error(manager='', operation=operation, details=result_msg,
error_code=error_code,
related_error_codes=related_error_codes,
@ -314,7 +321,9 @@ class NSX3Client(JSONRESTClient):
def _raise_error(self, status_code, operation, result_msg,
error_code=None, related_error_codes=None):
"""Override the Rest client errors to add the manager IPs"""
error = http_error_to_exception(status_code, error_code)
error = http_error_to_exception(
status_code, error_code,
related_error_codes=related_error_codes)
raise error(manager=self.nsx_api_managers,
operation=operation,
details=result_msg,