Skip to content

Commit

Permalink
Return a 422 response when a ValidationError is raised
Browse files Browse the repository at this point in the history
closes #38
  • Loading branch information
sloria committed Apr 15, 2015
1 parent aaed1f9 commit c435ad6
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions tests/test_bottleparser.py
Expand Up @@ -62,7 +62,7 @@ def test_parsing_form_default(testapp):

def test_abort_called_on_validation_error(testapp):
res = testapp.post('/echo', {'name': 'b'}, expect_errors=True)
assert res.status_code == 400
assert res.status_code == 422

def test_validation_error_with_status_code_and_data(app):
def always_fail(value):
Expand Down Expand Up @@ -91,7 +91,7 @@ def echo(args):
result = testapp.post('/foo/', {'myvalue': 43}, expect_errors=True)
assert result.status_code == 200
result = testapp.post('/foo/', {'myvalue': 41}, expect_errors=True)
assert result.status_code == 400
assert result.status_code == 422

def test_use_args_with_url_params(app, testapp):
@app.route('/foo/<name>')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_flaskparser.py
Expand Up @@ -155,7 +155,7 @@ def validate(x):
parser.parse(argmap)
mock_abort.assert_called
abort_args, abort_kwargs = mock_abort.call_args
assert abort_args[0] == 400
assert abort_args[0] == 422
expected_msg = u'Validator {0}({1}) is not True'.format(validate.__name__, 41)
assert abort_kwargs['message'] == expected_msg
assert type(abort_kwargs['exc']) == ValidationError
Expand Down Expand Up @@ -189,7 +189,7 @@ def echo(args):

test_client = testapp.test_client()
res = test_client.post('/foo/', data={'myvalue': 41})
assert res.status_code == 400
assert res.status_code == 422


def test_use_args_decorator_on_a_method(testapp):
Expand Down Expand Up @@ -296,7 +296,7 @@ def test_abort_called_when_required_arg_not_present(mock_abort, testapp):
with testapp.test_request_context('/foo', method='post',
data=json.dumps({}), content_type='application/json'):
parser.parse(args)
assert mock_abort.called_once_with(400)
assert mock_abort.called_once_with(422)

def test_arg_allowed_missing(testapp):
# 'last' argument is allowed to be missing
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pyramidparser.py
Expand Up @@ -123,7 +123,7 @@ def test_parse_files(testapp):

def test_exception_on_validation_error(testapp):
res = testapp.post('/validate', {'num': '3'}, expect_errors=True)
assert res.status_code == 400
assert res.status_code == 422

def test_validation_error_with_message(testapp):
res = testapp.post('/validate', {'num': '3'}, expect_errors=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tornadoparser.py
Expand Up @@ -625,14 +625,14 @@ def test_required_field_provided(self):
json_body = parse_json(res.body)
assert json_body['name'] == 'johnny'

def test_missing_required_field_throws_400(self):
def test_missing_required_field_throws_422(self):
res = self.fetch(
'/echo',
method='POST',
headers={'Content-Type': 'application/json'},
body=json.dumps({'occupation': 'pizza'}),
)
assert res.code == 400
assert res.code == 422

def test_validation_error_with_status_code_and_extra_data(self):
res = self.fetch(
Expand Down
2 changes: 1 addition & 1 deletion webargs/core.py
Expand Up @@ -40,7 +40,7 @@ class ValidationError(WebargsError):
Store status_code and additonal data.
"""
def __init__(self, error, status_code=400, arg_name=None, **data):
def __init__(self, error, status_code=422, arg_name=None, **data):
self.message = text_type(error)
self.status_code = status_code
self.arg_name = arg_name
Expand Down
6 changes: 5 additions & 1 deletion webargs/tornadoparser.py
Expand Up @@ -101,8 +101,12 @@ def handle_error(self, error):
"""
logger.error(error)
status_code = getattr(error, 'status_code', 400)
if status_code == 422:
reason = 'Unprocessable Entity'
else:
reason = None
data = getattr(error, 'data', {})
raise tornado.web.HTTPError(status_code, error.args[0], **data)
raise tornado.web.HTTPError(status_code, error.args[0], reason=reason, **data)

def use_args(self, argmap, req=None, locations=core.Parser.DEFAULT_LOCATIONS,
as_kwargs=False, validate=None):
Expand Down

0 comments on commit c435ad6

Please sign in to comment.