Skip to content

Commit 3946edb

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Display neutron api error message more precisely"
2 parents 17b9e2d + 23f4aa8 commit 3946edb

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

openstack/session.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,18 @@ def map_exceptions_wrapper(*args, **kwargs):
7070
url=e.url, method=e.method,
7171
http_status=e.http_status, cause=e)
7272
else:
73+
message = e.message
74+
details = e.details
75+
76+
if e.response is not None:
77+
body = e.response.json()
78+
if 'NeutronError' in body:
79+
err_dict = body['NeutronError']
80+
message = err_dict.get('message') or message
81+
details = err_dict.get('detail') or details
82+
7383
raise exceptions.HttpException(
74-
message=e.message, details=e.details,
84+
message=message, details=details,
7585
response=e.response, request_id=e.request_id,
7686
url=e.url, method=e.method,
7787
http_status=e.http_status, cause=e)

openstack/tests/functional/network/v2/test_router.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import uuid
1414

15+
from openstack import exceptions
1516
from openstack.network.v2 import router
1617
from openstack.tests.functional import base
1718

@@ -54,3 +55,20 @@ def test_list(self):
5455
def test_update(self):
5556
sot = self.conn.network.update_router(self.ID, name=self.UPDATE_NAME)
5657
self.assertEqual(self.UPDATE_NAME, sot.name)
58+
59+
def test_error(self):
60+
destination = "10.10.10.0/24"
61+
nexthop = "192.168.0.13"
62+
message = "Invalid input for routes. Reason: Invalid data format " \
63+
"for hostroute: '{u'destination': u'%s', " \
64+
"u'nexthop': u'%s'}'." % (destination, nexthop)
65+
66+
with self.assertRaises(exceptions.HttpException) as cm:
67+
routes = {
68+
"destination": destination,
69+
"nexthop": nexthop,
70+
}
71+
72+
self.conn.network.update_router(self.ID, routes=routes)
73+
74+
self.assertEqual(message, cm.exception.message)

openstack/tests/unit/test_session.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ def test_map_exceptions_http_exception(self):
8282
self.assertEqual(ksa_exc.http_status, os_exc.http_status)
8383
self.assertEqual(ksa_exc, os_exc.cause)
8484

85+
def test_map_exceptions_neutron_exception(self):
86+
fake_response = mock.Mock()
87+
message = "neutron error message"
88+
detail = "neutron detailed message"
89+
body = {
90+
"NeutronError": {
91+
"message": message,
92+
"detail": detail,
93+
}
94+
}
95+
96+
fake_response.json = mock.Mock(return_value=body)
97+
98+
ksa_exc = _exceptions.HttpError(response=fake_response)
99+
func = mock.Mock(side_effect=ksa_exc)
100+
101+
os_exc = self.assertRaises(
102+
exceptions.HttpException, session.map_exceptions(func))
103+
104+
self.assertEqual(message, os_exc.message)
105+
self.assertEqual(detail, os_exc.details)
106+
85107
def test_map_exceptions_sdk_exception_1(self):
86108
ksa_exc = _exceptions.ClientException()
87109
func = mock.Mock(side_effect=ksa_exc)

0 commit comments

Comments
 (0)