Fix Bad except clauses order

Invalid is an ancestor class of AutoDiskConfigDisabledByImage.
If we catch Invalid before, the user will be see a wrong message
"Invalid instance image." instead of "Requested Image has automatic
disk resize disabled."

Change-Id: Idb021c7da01ad7a1af07585be527773d14ff2d20
This commit is contained in:
Davanum Srinivas 2014-09-26 08:09:41 -04:00
parent a781a392eb
commit a9a80ad665
2 changed files with 20 additions and 6 deletions

View File

@ -1194,12 +1194,12 @@ class Controller(wsgi.Controller):
msg = _("Image that the instance was started "
"with could not be found.")
raise exc.HTTPBadRequest(explanation=msg)
except exception.Invalid:
msg = _("Invalid instance image.")
raise exc.HTTPBadRequest(explanation=msg)
except (exception.NoValidHost,
exception.AutoDiskConfigDisabledByImage) as e:
raise exc.HTTPBadRequest(explanation=e.format_message())
except exception.Invalid:
msg = _("Invalid instance image.")
raise exc.HTTPBadRequest(explanation=msg)
return webob.Response(status_int=202)

View File

@ -832,6 +832,10 @@ class ServerActionsControllerTest(test.TestCase):
(exception.ImageNotFound(image_id=image_id),
webob.exc.HTTPBadRequest),
(exception.Invalid, webob.exc.HTTPBadRequest),
(exception.NoValidHost(reason='Bad host'),
webob.exc.HTTPBadRequest),
(exception.AutoDiskConfigDisabledByImage(image=image_id),
webob.exc.HTTPBadRequest),
]
raised, expected = map(iter, zip(*exceptions))
@ -844,9 +848,19 @@ class ServerActionsControllerTest(test.TestCase):
for call_no in range(len(exceptions)):
req = fakes.HTTPRequest.blank(self.url)
self.assertRaises(expected.next(),
self.controller._action_resize,
req, FAKE_UUID, body)
next_exception = expected.next()
actual = self.assertRaises(next_exception,
self.controller._action_resize,
req, FAKE_UUID, body)
if (isinstance(exceptions[call_no][0],
exception.NoValidHost)):
self.assertEqual(actual.explanation,
'No valid host was found. Bad host')
elif (isinstance(exceptions[call_no][0],
exception.AutoDiskConfigDisabledByImage)):
self.assertEqual(actual.explanation,
'Requested image fake_image_id has automatic'
' disk resize disabled.')
self.assertEqual(self.resize_called, call_no + 1)
@mock.patch('nova.compute.api.API.resize',