Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only accept known locations in request.errors.add() (fixes #99) #374

Merged
merged 2 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Breaking changes

- Get rid of Buildout files (#369)
- Errors list ``request.errors`` has no ``request`` anymore (fixes #328)
- ``request.errors.add()`` now only accepts one of ``header``, ``body``, ``url``,
``path``, ``querystring``, ``cookies`` or ``method`` as first argument (fixes #99)

Bug fixes

Expand Down
5 changes: 5 additions & 0 deletions cornice/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def __init__(self, status=400):

def add(self, location, name=None, description=None, **kw):
"""Registers a new error."""
allowed = ('body', 'querystring', 'url', 'header', 'path',
'cookies', 'method')
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests for each accepted parameters?

2 changes: 1 addition & 1 deletion docs/source/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ This means something like this::
def validate_it(self, request, **kw):
# 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