Change status code from 500 to 400 for image update request

Invalid 'op' (operation) value provided for image update PATCH request
results in raising Internal Server Error (500). There is no helpful
information what went wrong and how to fix this particular request.
Proposed fix change the status code from 500 to 400 and return proper
message, so the user won't be confused and will be able to adjust the
request.

Change-Id: Ie20a3f350cbaa46c558b4e49b46091d1e4a74807
Closes-Bug: 1456157
This commit is contained in:
Kamil Rykowski 2015-05-18 13:19:54 +02:00
parent e16f50f220
commit 9f9629ada5
2 changed files with 16 additions and 5 deletions

View File

@ -329,6 +329,8 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
_default_sort_dir = 'desc'
_supported_operations = ('add', 'remove', 'replace')
def __init__(self, schema=None):
super(RequestDeserializer, self).__init__()
self.schema = schema or get_schema()
@ -372,15 +374,23 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
return dict(image=image, extra_properties=properties, tags=tags)
def _get_change_operation_d10(self, raw_change):
try:
return raw_change['op']
except KeyError:
msg = _("Unable to find '%s' in JSON Schema change") % 'op'
op = raw_change.get('op')
if op is None:
msg = _('Unable to find `op` in JSON Schema change. '
'It must be one of the following: %(available)s.') % \
{'available': ', '.join(self._supported_operations)}
raise webob.exc.HTTPBadRequest(explanation=msg)
if op not in self._supported_operations:
msg = _('Invalid operation: `%(op)s`. '
'It must be one of the following: %(available)s.') % \
{'op': op,
'available': ', '.join(self._supported_operations)}
raise webob.exc.HTTPBadRequest(explanation=msg)
return op
def _get_change_operation_d4(self, raw_change):
op = None
for key in ['replace', 'add', 'remove']:
for key in self._supported_operations:
if key in raw_change:
if op is not None:
msg = _('Operation objects must contain only one member'

View File

@ -2191,6 +2191,7 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
True,
False,
None,
{'op': 'invalid', 'path': '/name', 'value': 'fedora'}
]
for change in changes:
request = self._get_fake_patch_request()