Fix 500 on ValueError during image-import

ValueError is being raised during image-import,
and it's not being caught, leading to a 500. So this
change catches the ValueError and raises a HTTPBadRequest.

Change-Id: Ide4ca9492b9296c74f8302d2d12e385bb5ad663b
Closes-Bug: #1733810
This commit is contained in:
Abhishek Kekane 2017-11-24 05:18:44 +00:00 committed by Brian Rosmaita
parent cf0e0fa6f2
commit d8bcf8ba13
2 changed files with 18 additions and 0 deletions

View File

@ -111,6 +111,12 @@ class ImagesController(object):
raise webob.exc.HTTPConflict(explanation=e.msg)
except exception.InvalidImageStatusTransition as e:
raise webob.exc.HTTPConflict(explanation=e.msg)
except ValueError as e:
LOG.debug("Cannot import data for image %(id)s: %(e)s",
{'id': image_id,
'e': encodeutils.exception_to_unicode(e)})
raise webob.exc.HTTPBadRequest(
explanation=encodeutils.exception_to_unicode(e))
return image_id

View File

@ -625,6 +625,18 @@ class TestImagesController(base.IsolatedUnitTest):
self.controller.import_image, request, UUID4,
{})
def test_image_import_raises_bad_request(self):
request = unit_test_utils.get_fake_request()
# NOTE(abhishekk): Due to
# https://bugs.launchpad.net/glance/+bug/1712463 taskflow is not
# executing. Once it is fixed instead of mocking spawn_n method
# we should mock execute method of _ImportToStore task.
with mock.patch.object(eventlet.GreenPool, 'spawn_n',
side_effect=ValueError):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.import_image, request, UUID4,
{})
def test_create(self):
request = unit_test_utils.get_fake_request()
image = {'name': 'image-1'}