Return related error code with ManagerError

For error handler to decide the exact error type,
related error code is needed. The field will be empty
list if there is not related error

Issue: #

Change-Id: I53ad9e6da41ab65ea96414753635576c64d5d6d0
This commit is contained in:
Erica Liu
2019-01-22 16:56:23 -08:00
committed by Abhishek Raut
parent fb84d8721f
commit eedad6e61b
3 changed files with 18 additions and 6 deletions

View File

@@ -249,7 +249,10 @@ class NsxV3RESTClientTestCase(nsxlib_testcase.NsxClientTestCase):
content = jsonutils.dumps({'httpStatus': 'dummy', content = jsonutils.dumps({'httpStatus': 'dummy',
'error_code': error_code, 'error_code': error_code,
'module_name': 'dummy', 'module_name': 'dummy',
'error_message': 'bad'}) 'error_message': 'bad',
'related_errors': [{
'error_message': 'bla',
'error_code': 'code'}]})
response = mocks.MockRequestsResponse( response = mocks.MockRequestsResponse(
status_code, content) status_code, content)

View File

@@ -141,10 +141,11 @@ class RESTClient(object):
return self._rest_call(url, method='PATCH', body=body, headers=headers) return self._rest_call(url, method='PATCH', body=body, headers=headers)
def _raise_error(self, status_code, operation, result_msg, def _raise_error(self, status_code, operation, result_msg,
error_code=None): 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)
raise error(manager='', operation=operation, details=result_msg, raise error(manager='', operation=operation, details=result_msg,
error_code=error_code) error_code=error_code,
related_error_codes=related_error_codes)
def _validate_result(self, result, expected, operation, silent=False): def _validate_result(self, result, expected, operation, silent=False):
if result.status_code not in expected: if result.status_code not in expected:
@@ -159,16 +160,21 @@ class RESTClient(object):
'body': result_msg}) 'body': result_msg})
error_code = None error_code = None
related_error_codes = []
if isinstance(result_msg, dict) and 'error_message' in result_msg: if isinstance(result_msg, dict) and 'error_message' in result_msg:
error_code = result_msg.get('error_code') error_code = result_msg.get('error_code')
related_errors = [error['error_message'] for error in related_errors = [error['error_message'] for error in
result_msg.get('related_errors', [])] result_msg.get('related_errors', [])]
related_error_codes = [str(error['error_code']) for error in
result_msg.get('related_errors', []) if
error.get('error_code')]
result_msg = result_msg['error_message'] result_msg = result_msg['error_message']
if related_errors: if related_errors:
result_msg += " relatedErrors: %s" % ' '.join( result_msg += " relatedErrors: %s" % ' '.join(
related_errors) related_errors)
self._raise_error(result.status_code, operation, result_msg, self._raise_error(result.status_code, operation, result_msg,
error_code=error_code) error_code=error_code,
related_error_codes=related_error_codes)
@classmethod @classmethod
def merge_headers(cls, *headers): def merge_headers(cls, *headers):
@@ -297,13 +303,14 @@ class NSX3Client(JSONRESTClient):
client_obj=client_obj) client_obj=client_obj)
def _raise_error(self, status_code, operation, result_msg, def _raise_error(self, status_code, operation, result_msg,
error_code=None): error_code=None, related_error_codes=None):
"""Override the Rest client errors to add the manager IPs""" """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)
raise error(manager=self.nsx_api_managers, raise error(manager=self.nsx_api_managers,
operation=operation, operation=operation,
details=result_msg, details=result_msg,
error_code=error_code) error_code=error_code,
related_error_codes=related_error_codes)
def _rest_call(self, url, **kwargs): def _rest_call(self, url, **kwargs):
if self.rate_limit_retry and kwargs.get('with_retries', True): if self.rate_limit_retry and kwargs.get('with_retries', True):

View File

@@ -70,6 +70,7 @@ class NsxLibInvalidInput(NsxLibException):
class ManagerError(NsxLibException): class ManagerError(NsxLibException):
message = _("Unexpected error from backend manager (%(manager)s) " message = _("Unexpected error from backend manager (%(manager)s) "
"for %(operation)s%(details)s") "for %(operation)s%(details)s")
related_error_codes = []
def __init__(self, **kwargs): def __init__(self, **kwargs):
details = kwargs.get('details', '') details = kwargs.get('details', '')
@@ -80,6 +81,7 @@ class ManagerError(NsxLibException):
except KeyError: except KeyError:
self.msg = details self.msg = details
self.error_code = kwargs.get('error_code') self.error_code = kwargs.get('error_code')
self.related_error_codes = kwargs.get('related_error_codes', [])
class ResourceNotFound(ManagerError): class ResourceNotFound(ManagerError):