Skip to content

Commit

Permalink
Only accept known locations in request.errors.add() (fixes #99)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Aug 23, 2016
1 parent 981af99 commit 3527260
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CHANGELOG
Breaking changes

- Get rid of Buildout files (#369)
- ``request.errors.add()`` now only accepts one of ``header``, ``body``, ``url``,
``querystring`` as first argument (fixes #99)


1.2.1 (2016-03-15)
Expand Down
4 changes: 4 additions & 0 deletions cornice/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def __init__(self, request=None, status=400):

def add(self, location, name=None, description=None, **kw):
"""Registers a new error."""
allowed = ('body', 'querystring', 'url', 'header')
if location not in allowed:
raise ValueError('%r not in %s' % (location, allowed))

self.append(dict(
location=location,
name=name,
Expand Down
12 changes: 12 additions & 0 deletions cornice/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from cornice.tests.support import TestCase

from cornice.errors import Errors


class TestErrorsHelper(TestCase):
def setUp(self):
self.errors = Errors()

def test_raises_an_exception_when_location_is_unsupported(self):
with self.assertRaises(ValueError):
self.errors.add('something')
2 changes: 1 addition & 1 deletion docs/source/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ This means something like this::
def validate_it(request):
# pseudo-code validation logic
if whatever is wrong:
request.errors.add('something')
request.errors.add('body', description="Something is wrong")

@service.get(klass=MyClass, validators=('validate_it',))
def view(request):
Expand Down

0 comments on commit 3527260

Please sign in to comment.