Handle some exceptions of image_create v2 api

Currently, in image_create v2 api source we can see
ReservedProperty, ReadonlyProperty and TypeError caused in
glance/domain/__init__.py are not handled, which will cause
500 when these exceptions happens.The internal error should
be interpreted to the appropriate exception to the user.
This patch catches them and changes them to http exception.

Change-Id: I0c82a142b3357c8b8faab7dc0905967983cb8f7c
This commit is contained in:
Haiwei Xu 2014-09-12 13:31:17 +09:00 committed by xu-haiwei
parent 9bb7ec57fb
commit 194d275fd1
2 changed files with 42 additions and 0 deletions

View File

@ -75,6 +75,14 @@ class ImagesController(object):
explanation=e.msg, request=req, content_type='text/plain')
except exception.Duplicate as dupex:
raise webob.exc.HTTPConflict(explanation=dupex.msg)
except exception.ReservedProperty as e:
raise webob.exc.HTTPForbidden(explanation=e.msg)
except exception.ReadonlyProperty as e:
raise webob.exc.HTTPForbidden(explanation=e.msg)
except TypeError as e:
LOG.debug(utils.exception_to_str(e))
raise webob.exc.HTTPBadRequest(
explanation=utils.exception_to_str(e))
return image

View File

@ -18,6 +18,7 @@ import os
import uuid
import glance_store as store
import mock
from oslo.config import cfg
from oslo.serialization import jsonutils
import six
@ -26,6 +27,7 @@ import webob
import glance.api.v2.images
from glance.common import exception
from glance import domain
import glance.schema
from glance.tests.unit import base
import glance.tests.unit.utils as unit_test_utils
@ -683,6 +685,38 @@ class TestImagesController(base.IsolatedUnitTest):
request, image=image, extra_properties={},
tags=[])
def test_create_unexpected_property(self):
request = unit_test_utils.get_fake_request()
image_properties = {'unexpected': 'unexpected'}
image = {'name': 'image-1'}
with mock.patch.object(domain.ImageFactory, 'new_image',
side_effect=TypeError):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.create, request, image=image,
extra_properties=image_properties, tags=[])
def test_create_reserved_property(self):
request = unit_test_utils.get_fake_request()
image_properties = {'reserved': 'reserved'}
image = {'name': 'image-1'}
with mock.patch.object(domain.ImageFactory, 'new_image',
side_effect=exception.ReservedProperty(
property='reserved')):
self.assertRaises(webob.exc.HTTPForbidden,
self.controller.create, request, image=image,
extra_properties=image_properties, tags=[])
def test_create_readonly_property(self):
request = unit_test_utils.get_fake_request()
image_properties = {'readonly': 'readonly'}
image = {'name': 'image-1'}
with mock.patch.object(domain.ImageFactory, 'new_image',
side_effect=exception.ReadonlyProperty(
property='readonly')):
self.assertRaises(webob.exc.HTTPForbidden,
self.controller.create, request, image=image,
extra_properties=image_properties, tags=[])
def test_update_no_changes(self):
request = unit_test_utils.get_fake_request()
output = self.controller.update(request, UUID1, changes=[])