Return 400 for invalid Json for api v2

For invalid json, a ValueError is returned by python's json decoder.
On v2, we now catch this and return a 400 (invalid_json)

Change-Id: Idb76f051c6d09d5fd26f0a44fa75a0ffd9f37b6a
Closes-Bug: #1288456
This commit is contained in:
Vinod Mangalpally 2014-03-05 18:58:17 -06:00
parent 57d24f3858
commit 877055aca7
2 changed files with 9 additions and 1 deletions

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import pecan.core
from designate import exceptions
from designate.openstack.common import jsonutils
JSON_TYPES = ('application/json', 'application/json-patch+json')
@ -30,7 +31,10 @@ class Request(pecan.core.Request):
is not hardcoded to call pecans "request.json()" method.
"""
if self.content_type in JSON_TYPES:
return jsonutils.load(self.body_file)
try:
return jsonutils.load(self.body_file)
except ValueError as valueError:
raise exceptions.InvalidJson(valueError.message)
else:
raise Exception('TODO: Unsupported Content Type')

View File

@ -115,6 +115,10 @@ class InvalidSortKey(BadRequest):
error_type = 'invalid_sort_key'
class InvalidJson(BadRequest):
error_type = 'invalid_json'
class InvalidOperation(BadRequest):
error_code = 400
error_type = 'invalid_operation'