]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a distinct MissingArgumentError class for get_argument.
authorBen Darnell <ben@bendarnell.com>
Sun, 12 May 2013 16:47:27 +0000 (12:47 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 12 May 2013 16:55:00 +0000 (12:55 -0400)
Closes #782.

docs/web.rst
tornado/test/web_test.py
tornado/web.py

index cfc9d480dc8cf9e93f8a5d35195293e46d4c3680..585047f681503a0804f0f865e7ef1859839aa839 100644 (file)
    Everything else
    ---------------
    .. autoexception:: HTTPError
+   .. autoexception:: MissingArgumentError
    .. autoclass:: UIModule
       :members:
 
index 7c5256e8238f40c63dd7b92348ce8322146ded00..6f528127144172b99cee64572f1f553785af8a65 100644 (file)
@@ -9,7 +9,7 @@ from tornado.template import DictLoader
 from tornado.testing import AsyncHTTPTestCase, ExpectLog
 from tornado.test.util import unittest
 from tornado.util import u, bytes_type, ObjectDict, unicode_type
-from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature, create_signed_value, ErrorHandler, UIModule
+from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature, create_signed_value, ErrorHandler, UIModule, MissingArgumentError
 
 import binascii
 import datetime
@@ -1218,3 +1218,21 @@ class UIMethodUIModuleTest(SimpleHandlerTestCase):
         self.assertEqual(response.body,
                          b'In my_ui_method(42) with handler value asdf. '
                          b'In MyModule(123) with handler value asdf.')
+
+
+@wsgi_safe
+class GetArgumentErrorTest(SimpleHandlerTestCase):
+    class Handler(RequestHandler):
+        def get(self):
+            try:
+                self.get_argument('foo')
+                self.write({})
+            except MissingArgumentError as e:
+                self.write({'arg_name': e.arg_name,
+                            'log_message': e.log_message})
+
+    def test_catch_error(self):
+        response = self.fetch('/')
+        self.assertEqual(json_decode(response.body),
+                         {'arg_name': 'foo',
+                          'log_message': 'Missing argument foo'})
index 52c9eb18a45027b06e77b4de183f75b626081a16..c43bca45c63cd9981914470ba2d5a19280f91402 100644 (file)
@@ -328,7 +328,7 @@ class RequestHandler(object):
         """Returns the value of the argument with the given name.
 
         If default is not provided, the argument is considered to be
-        required, and we throw an HTTP 400 exception if it is missing.
+        required, and we raise a `MissingArgumentError` if it is missing.
 
         If the argument appears in the url more than once, we return the
         last value.
@@ -338,7 +338,7 @@ class RequestHandler(object):
         args = self.get_arguments(name, strip=strip)
         if not args:
             if default is self._ARG_DEFAULT:
-                raise HTTPError(400, "Missing argument %s" % name)
+                raise MissingArgumentError(name)
             return default
         return args[-1]
 
@@ -1575,6 +1575,18 @@ class HTTPError(Exception):
             return message
 
 
+class MissingArgumentError(HTTPError):
+    """Exception raised by `RequestHandler.get_argument`.
+
+    This is a subclass of `HTTPError`, so if it is uncaught a 400 response
+    code will be used instead of 500 (and a stack trace will not be logged).
+    """
+    def __init__(self, arg_name):
+        super(MissingArgumentError, self).__init__(
+            400, 'Missing argument %s' % arg_name)
+        self.arg_name = arg_name
+
+
 class ErrorHandler(RequestHandler):
     """Generates an error response with ``status_code`` for all requests."""
     def initialize(self, status_code):