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:
parent
e16f50f220
commit
9f9629ada5
@ -329,6 +329,8 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
|
|||||||
|
|
||||||
_default_sort_dir = 'desc'
|
_default_sort_dir = 'desc'
|
||||||
|
|
||||||
|
_supported_operations = ('add', 'remove', 'replace')
|
||||||
|
|
||||||
def __init__(self, schema=None):
|
def __init__(self, schema=None):
|
||||||
super(RequestDeserializer, self).__init__()
|
super(RequestDeserializer, self).__init__()
|
||||||
self.schema = schema or get_schema()
|
self.schema = schema or get_schema()
|
||||||
@ -372,15 +374,23 @@ class RequestDeserializer(wsgi.JSONRequestDeserializer):
|
|||||||
return dict(image=image, extra_properties=properties, tags=tags)
|
return dict(image=image, extra_properties=properties, tags=tags)
|
||||||
|
|
||||||
def _get_change_operation_d10(self, raw_change):
|
def _get_change_operation_d10(self, raw_change):
|
||||||
try:
|
op = raw_change.get('op')
|
||||||
return raw_change['op']
|
if op is None:
|
||||||
except KeyError:
|
msg = _('Unable to find `op` in JSON Schema change. '
|
||||||
msg = _("Unable to find '%s' in JSON Schema change") % 'op'
|
'It must be one of the following: %(available)s.') % \
|
||||||
|
{'available': ', '.join(self._supported_operations)}
|
||||||
raise webob.exc.HTTPBadRequest(explanation=msg)
|
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):
|
def _get_change_operation_d4(self, raw_change):
|
||||||
op = None
|
op = None
|
||||||
for key in ['replace', 'add', 'remove']:
|
for key in self._supported_operations:
|
||||||
if key in raw_change:
|
if key in raw_change:
|
||||||
if op is not None:
|
if op is not None:
|
||||||
msg = _('Operation objects must contain only one member'
|
msg = _('Operation objects must contain only one member'
|
||||||
|
@ -2191,6 +2191,7 @@ class TestImagesDeserializer(test_utils.BaseTestCase):
|
|||||||
True,
|
True,
|
||||||
False,
|
False,
|
||||||
None,
|
None,
|
||||||
|
{'op': 'invalid', 'path': '/name', 'value': 'fedora'}
|
||||||
]
|
]
|
||||||
for change in changes:
|
for change in changes:
|
||||||
request = self._get_fake_patch_request()
|
request = self._get_fake_patch_request()
|
||||||
|
Loading…
Reference in New Issue
Block a user