Print pecan exceptions properly

Exceptions raised by pecan (for example the ones happening in _lookup
methods of ironic controllers) are not properly handled, as the
fields that contain the error details are different from wsme raised
exceptions. This change fixes it.

Closes-Bug: #1619471
Change-Id: I023870ed97d3e97df8ae67e7cec97d6fbf355e79
This commit is contained in:
Vladyslav Drok 2016-09-02 00:31:24 +03:00 committed by Julia Kreger
parent 9a67f1a316
commit fbcaf92521
3 changed files with 48 additions and 7 deletions

View File

@ -366,8 +366,12 @@ class HTTPClient(VersionNegotiationMixin):
if resp.status_code >= http_client.BAD_REQUEST:
error_json = _extract_error_json(body_str)
# NOTE(vdrok): exceptions from ironic controllers' _lookup methods
# are constructed directly by pecan instead of wsme, and contain
# only description field
raise exc.from_response(
resp, error_json.get('faultstring'),
resp, (error_json.get('faultstring') or
error_json.get('description')),
error_json.get('debuginfo'), method, url)
elif resp.status_code in (http_client.MOVED_PERMANENTLY,
http_client.FOUND,
@ -530,7 +534,11 @@ class SessionClient(VersionNegotiationMixin, adapter.LegacyJsonAdapter):
return self._http_request(url, method, **kwargs)
if resp.status_code >= http_client.BAD_REQUEST:
error_json = _extract_error_json(resp.content)
raise exc.from_response(resp, error_json.get('faultstring'),
# NOTE(vdrok): exceptions from ironic controllers' _lookup methods
# are constructed directly by pecan instead of wsme, and contain
# only description field
raise exc.from_response(resp, (error_json.get('faultstring') or
error_json.get('description')),
error_json.get('debuginfo'), method, url)
elif resp.status_code in (http_client.MOVED_PERMANENTLY,
http_client.FOUND, http_client.USE_PROXY):

View File

@ -35,11 +35,14 @@ DEFAULT_HOST = 'localhost'
DEFAULT_PORT = '1234'
def _get_error_body(faultstring=None, debuginfo=None):
error_body = {
'faultstring': faultstring,
'debuginfo': debuginfo
}
def _get_error_body(faultstring=None, debuginfo=None, description=None):
if description:
error_body = {'description': description}
else:
error_body = {
'faultstring': faultstring,
'debuginfo': debuginfo
}
raw_error_body = jsonutils.dump_as_bytes(error_body)
body = {'error_message': raw_error_body}
return jsonutils.dumps(body)
@ -243,6 +246,20 @@ class HttpClientTest(utils.BaseTestCase):
client.json_request,
'GET', '/v1/resources')
def test_server_exception_description_only(self):
error_msg = 'test error msg'
error_body = _get_error_body(description=error_msg)
client = http.HTTPClient('http://localhost/')
client.session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body,
version=1,
status_code=http_client.BAD_REQUEST)
self.assertRaisesRegex(exc.BadRequest, 'test error msg',
client.json_request,
'GET', '/v1/resources')
def test_server_https_request_ok(self):
client = http.HTTPClient('https://localhost/')
client.session = utils.mockSession(
@ -454,6 +471,18 @@ class SessionClientTest(utils.BaseTestCase):
client.json_request,
'GET', '/v1/resources')
def test_server_exception_description_only(self):
error_msg = 'test error msg'
error_body = _get_error_body(description=error_msg)
fake_session = utils.mockSession(
{'Content-Type': 'application/json'},
error_body, status_code=http_client.BAD_REQUEST)
client = _session_client(session=fake_session)
self.assertRaisesRegex(exc.BadRequest, 'test error msg',
client.json_request,
'GET', '/v1/resources')
def test__parse_version_headers(self):
# Test parsing of version headers from SessionClient
fake_session = utils.mockSession(

View File

@ -0,0 +1,4 @@
---
fixes:
- Fixes an issue where certain error messages from the
ironic API were suppressed.