Fix exceptions to be displayed properly
Valence exception messages are not displayed properly. This commit is to fix the same. Change-Id: If4da51f736d28e60a3cfa78094b2e60bedecdd83 Closes-Bug:#1715580
This commit is contained in:
parent
f16f4a40c2
commit
f5b935a7e1
@ -48,16 +48,7 @@ def _trim_endpoint_api_version(url):
|
|||||||
|
|
||||||
|
|
||||||
def _extract_error_json(body):
|
def _extract_error_json(body):
|
||||||
error_json = {}
|
return jsonutils.loads(body)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def with_retries(func):
|
def with_retries(func):
|
||||||
@ -153,9 +144,7 @@ class HTTPClient(object):
|
|||||||
body_iter = six.StringIO(resp.text)
|
body_iter = six.StringIO(resp.text)
|
||||||
if resp.status_code >= http_client.BAD_REQUEST:
|
if resp.status_code >= http_client.BAD_REQUEST:
|
||||||
error_json = _extract_error_json(resp.text)
|
error_json = _extract_error_json(resp.text)
|
||||||
raise exc.from_response(resp, error_json.get('faultstring'),
|
raise exc.from_response(resp, error_json, method, conn_url)
|
||||||
error_json.get('debugfino'), method,
|
|
||||||
conn_url)
|
|
||||||
elif resp.status_code in (http_client.FOUND,
|
elif resp.status_code in (http_client.FOUND,
|
||||||
http_client.USE_PROXY):
|
http_client.USE_PROXY):
|
||||||
return self._http_request(resp['location'], method, **kwargs)
|
return self._http_request(resp['location'], method, **kwargs)
|
||||||
|
@ -14,15 +14,11 @@
|
|||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from valenceclient.common.apiclient import exceptions
|
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
|
BadRequest = exceptions.BadRequest
|
||||||
ClientException = ClientException
|
ClientException = exceptions.ClientException
|
||||||
InternalServerError = InternalServerError
|
InternalServerError = exceptions.InternalServerError
|
||||||
ValidationError = ValidationError
|
ValidationError = exceptions.ValidationError
|
||||||
|
|
||||||
|
|
||||||
class InvalidValenceUrl(ClientException):
|
class InvalidValenceUrl(ClientException):
|
||||||
@ -45,15 +41,10 @@ class ConnectionRefuse(ClientException):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def from_response(response, message=None, traceback=None, method=None,
|
def from_response(response, error=None, method=None, url=None):
|
||||||
url=None):
|
|
||||||
"""Return an HttpError instance based on response from httplib/requests"""
|
"""Return an HttpError instance based on response from httplib/requests"""
|
||||||
|
|
||||||
error_body = {}
|
error_body = {}
|
||||||
if message:
|
error_body['message'] = error['detail']
|
||||||
error_body['message'] = message
|
|
||||||
if traceback:
|
|
||||||
error_body['traceback'] = traceback
|
|
||||||
|
|
||||||
if hasattr(response, 'status') and not hasattr(response, 'status_code'):
|
if hasattr(response, 'status') and not hasattr(response, 'status_code'):
|
||||||
response.status_code = response.status
|
response.status_code = response.status
|
||||||
|
@ -26,18 +26,19 @@ class ExcTest(test_utils.BaseTestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(ExcTest, self).setUp()
|
super(ExcTest, self).setUp()
|
||||||
self.message = 'SpongeBob SquarePants'
|
self.error = {'code': 'Bad Request',
|
||||||
self.traceback = 'Foo Traceback'
|
'status': '400',
|
||||||
|
'detail': 'unsupported parameters',
|
||||||
|
'title': 'BadRequest'}
|
||||||
|
|
||||||
self.method = 'call_spongebob'
|
self.method = 'call_spongebob'
|
||||||
self.url = 'http://foo.bar'
|
self.url = 'http://foo.bar'
|
||||||
self.expected_json = {'error': {'message': self.message,
|
self.expected_json = {'error': {'message': 'unsupported parameters'}}
|
||||||
'traceback': self.traceback}}
|
|
||||||
|
|
||||||
def test_from_response(self, mock_apiclient):
|
def test_from_response(self, mock_apiclient):
|
||||||
fake_response = mock.Mock(status_code=http_client.BAD_REQUEST)
|
fake_response = mock.Mock(status_code=http_client.BAD_REQUEST)
|
||||||
exc.from_response(fake_response, message=self.message,
|
exc.from_response(fake_response, error=self.error,
|
||||||
traceback=self.traceback, method=self.method,
|
method=self.method, url=self.url)
|
||||||
url=self.url)
|
|
||||||
self.assertEqual(http_client.BAD_REQUEST, fake_response.status_code)
|
self.assertEqual(http_client.BAD_REQUEST, fake_response.status_code)
|
||||||
self.assertEqual(self.expected_json, fake_response.json())
|
self.assertEqual(self.expected_json, fake_response.json())
|
||||||
mock_apiclient.assert_called_once_with(
|
mock_apiclient.assert_called_once_with(
|
||||||
@ -48,8 +49,7 @@ class ExcTest(test_utils.BaseTestCase):
|
|||||||
fake_response.getheader.return_value = 'fake-header'
|
fake_response.getheader.return_value = 'fake-header'
|
||||||
delattr(fake_response, 'status_code')
|
delattr(fake_response, 'status_code')
|
||||||
|
|
||||||
exc.from_response(fake_response, message=self.message,
|
exc.from_response(fake_response, error=self.error, method=self.method,
|
||||||
traceback=self.traceback, method=self.method,
|
|
||||||
url=self.url)
|
url=self.url)
|
||||||
expected_header = {'Content-Type': 'fake-header'}
|
expected_header = {'Content-Type': 'fake-header'}
|
||||||
self.assertEqual(expected_header, fake_response.headers)
|
self.assertEqual(expected_header, fake_response.headers)
|
||||||
|
@ -27,17 +27,13 @@ DEFAULT_HOST = 'localhost'
|
|||||||
DEFAULT_POST = '1234'
|
DEFAULT_POST = '1234'
|
||||||
|
|
||||||
|
|
||||||
def _get_error_body(faultstring=None, debuginfo=None, description=None):
|
def _get_error_body(detail=None):
|
||||||
if description:
|
error = {'code': 'Bad Request',
|
||||||
error_body = {'description': description}
|
'status': '400',
|
||||||
else:
|
'detail': detail or 'unsupported params',
|
||||||
error_body = {
|
'title': 'BadRequest'}
|
||||||
'faultstring': faultstring,
|
|
||||||
'debuginfo': debuginfo
|
return jsonutils.dumps(error)
|
||||||
}
|
|
||||||
raw_error_body = jsonutils.dump_as_bytes(error_body)
|
|
||||||
body = {'error_message': raw_error_body}
|
|
||||||
return jsonutils.dumps(body)
|
|
||||||
|
|
||||||
|
|
||||||
class HttpClientTest(utils.BaseTestCase):
|
class HttpClientTest(utils.BaseTestCase):
|
||||||
@ -76,6 +72,7 @@ class HttpClientTest(utils.BaseTestCase):
|
|||||||
|
|
||||||
def test_server_exception_msg_only(self):
|
def test_server_exception_msg_only(self):
|
||||||
error_body = "test error msg"
|
error_body = "test error msg"
|
||||||
|
error_body = _get_error_body(detail=error_body)
|
||||||
kwargs = {"valence_url": "http://localhost"}
|
kwargs = {"valence_url": "http://localhost"}
|
||||||
client = http.HTTPClient(**kwargs)
|
client = http.HTTPClient(**kwargs)
|
||||||
client.session = utils.mockSession(
|
client.session = utils.mockSession(
|
||||||
@ -90,7 +87,7 @@ class HttpClientTest(utils.BaseTestCase):
|
|||||||
|
|
||||||
def test_server_exception_description_only(self):
|
def test_server_exception_description_only(self):
|
||||||
error_msg = "test error msg"
|
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/"}
|
kwargs = {"valence_url": "http://localhost/"}
|
||||||
client = http.HTTPClient(**kwargs)
|
client = http.HTTPClient(**kwargs)
|
||||||
client.session = utils.mockSession(
|
client.session = utils.mockSession(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user