NSX v3 API client error propagation

Currently the NSX v3 REST API client masks the
backend NSX API error upon and invalid response.
This results in a very generic error message to consumers
(including the CLI) when a backend error happens.
This patch exposes the backend error message if
possible which provides more details of the issue
to consumers.

Change-Id: I215352d649b6579d6075cb104a7d311a7f1ffa66
This commit is contained in:
Boden R 2015-10-22 09:52:29 -06:00
parent 48661b25e5
commit a03cec98da
2 changed files with 15 additions and 3 deletions

View File

@ -127,7 +127,13 @@ class NoRouterAvailable(n_exc.ResourceExhausted):
class ManagerError(NsxPluginException): class ManagerError(NsxPluginException):
message = _("Unexpected error from backend manager (%(manager)s) " message = _("Unexpected error from backend manager (%(manager)s) "
"for %(operation)s") "for %(operation)s %(details)s")
def __init__(self, **kwargs):
kwargs['details'] = (': %s' % kwargs['details']
if 'details' in kwargs
else '')
super(ManagerError, self).__init__(**kwargs)
class ResourceNotFound(ManagerError): class ResourceNotFound(ManagerError):

View File

@ -101,17 +101,23 @@ class RESTClient(object):
def _validate_result(self, result, expected, operation): def _validate_result(self, result, expected, operation):
if result.status_code not in expected: if result.status_code not in expected:
result_msg = result.json() if result.content else ''
LOG.warning(_LW("The HTTP request returned error code " LOG.warning(_LW("The HTTP request returned error code "
"%(result)d, whereas %(expected)s response " "%(result)d, whereas %(expected)s response "
"codes were expected. Response body %(body)s"), "codes were expected. Response body %(body)s"),
{'result': result.status_code, {'result': result.status_code,
'expected': '/'.join([str(code) 'expected': '/'.join([str(code)
for code in expected]), for code in expected]),
'body': result.json() if result.content else ''}) 'body': result_msg})
manager_error = ERRORS.get( manager_error = ERRORS.get(
result.status_code, nsx_exc.ManagerError) result.status_code, nsx_exc.ManagerError)
raise manager_error(manager=self._host_ip, operation=operation) if type(result_msg) is dict:
result_msg = result_msg.get('error_message', result_msg)
raise manager_error(
manager=self._host_ip,
operation=operation,
details=result_msg)
@classmethod @classmethod
def merge_headers(cls, *headers): def merge_headers(cls, *headers):