Merge "Fix exceptions to be displayed properly"

This commit is contained in:
Jenkins 2017-09-21 18:03:44 +00:00 committed by Gerrit Code Review
commit 85c1f672ea
4 changed files with 26 additions and 49 deletions

View File

@ -48,16 +48,7 @@ def _trim_endpoint_api_version(url):
def _extract_error_json(body):
error_json = {}
try:
body_json = jsonutils.loads(body)
if 'error_message' in body_json:
raw_msg = body_json['error_message']
error_json = jsonutils.loads(raw_msg)
except ValueError:
pass
return error_json
return jsonutils.loads(body)
def with_retries(func):
@ -153,9 +144,7 @@ class HTTPClient(object):
body_iter = six.StringIO(resp.text)
if resp.status_code >= http_client.BAD_REQUEST:
error_json = _extract_error_json(resp.text)
raise exc.from_response(resp, error_json.get('faultstring'),
error_json.get('debugfino'), method,
conn_url)
raise exc.from_response(resp, error_json, method, conn_url)
elif resp.status_code in (http_client.FOUND,
http_client.USE_PROXY):
return self._http_request(resp['location'], method, **kwargs)

View File

@ -14,15 +14,11 @@
# under the License.
from valenceclient.common.apiclient import exceptions
from valenceclient.common.apiclient.exceptions import BadRequest
from valenceclient.common.apiclient.exceptions import ClientException
from valenceclient.common.apiclient.exceptions import InternalServerError
from valenceclient.common.apiclient.exceptions import ValidationError
BadRequest = BadRequest
ClientException = ClientException
InternalServerError = InternalServerError
ValidationError = ValidationError
BadRequest = exceptions.BadRequest
ClientException = exceptions.ClientException
InternalServerError = exceptions.InternalServerError
ValidationError = exceptions.ValidationError
class InvalidValenceUrl(ClientException):
@ -45,15 +41,10 @@ class ConnectionRefuse(ClientException):
pass
def from_response(response, message=None, traceback=None, method=None,
url=None):
def from_response(response, error=None, method=None, url=None):
"""Return an HttpError instance based on response from httplib/requests"""
error_body = {}
if message:
error_body['message'] = message
if traceback:
error_body['traceback'] = traceback
error_body['message'] = error['detail']
if hasattr(response, 'status') and not hasattr(response, 'status_code'):
response.status_code = response.status

View File

@ -26,18 +26,19 @@ class ExcTest(test_utils.BaseTestCase):
def setUp(self):
super(ExcTest, self).setUp()
self.message = 'SpongeBob SquarePants'
self.traceback = 'Foo Traceback'
self.error = {'code': 'Bad Request',
'status': '400',
'detail': 'unsupported parameters',
'title': 'BadRequest'}
self.method = 'call_spongebob'
self.url = 'http://foo.bar'
self.expected_json = {'error': {'message': self.message,
'traceback': self.traceback}}
self.expected_json = {'error': {'message': 'unsupported parameters'}}
def test_from_response(self, mock_apiclient):
fake_response = mock.Mock(status_code=http_client.BAD_REQUEST)
exc.from_response(fake_response, message=self.message,
traceback=self.traceback, method=self.method,
url=self.url)
exc.from_response(fake_response, error=self.error,
method=self.method, url=self.url)
self.assertEqual(http_client.BAD_REQUEST, fake_response.status_code)
self.assertEqual(self.expected_json, fake_response.json())
mock_apiclient.assert_called_once_with(
@ -48,8 +49,7 @@ class ExcTest(test_utils.BaseTestCase):
fake_response.getheader.return_value = 'fake-header'
delattr(fake_response, 'status_code')
exc.from_response(fake_response, message=self.message,
traceback=self.traceback, method=self.method,
exc.from_response(fake_response, error=self.error, method=self.method,
url=self.url)
expected_header = {'Content-Type': 'fake-header'}
self.assertEqual(expected_header, fake_response.headers)

View File

@ -27,17 +27,13 @@ DEFAULT_HOST = 'localhost'
DEFAULT_POST = '1234'
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)
def _get_error_body(detail=None):
error = {'code': 'Bad Request',
'status': '400',
'detail': detail or 'unsupported params',
'title': 'BadRequest'}
return jsonutils.dumps(error)
class HttpClientTest(utils.BaseTestCase):
@ -76,6 +72,7 @@ class HttpClientTest(utils.BaseTestCase):
def test_server_exception_msg_only(self):
error_body = "test error msg"
error_body = _get_error_body(detail=error_body)
kwargs = {"valence_url": "http://localhost"}
client = http.HTTPClient(**kwargs)
client.session = utils.mockSession(
@ -90,7 +87,7 @@ class HttpClientTest(utils.BaseTestCase):
def test_server_exception_description_only(self):
error_msg = "test error msg"
error_body = _get_error_body(description=error_msg)
error_body = _get_error_body(detail=error_msg)
kwargs = {"valence_url": "http://localhost/"}
client = http.HTTPClient(**kwargs)
client.session = utils.mockSession(