1. cherrypy
  2. CherryPy
Issue #733 resolved

404 instead of 500 on wrong number of arguments

Robert Brewer
created an issue

Currently, calling a handler with the wrong number of arguments results in a 500 error. A 404 might be better. The following patch (against trunk) tries to do this:

{{{ Index: _cpdispatch.py =================================================================== --- _cpdispatch.py (revision 1716) +++ _cpdispatch.py (working copy) @@ -21,7 +21,14 @@ self.kwargs = kwargs

 def __call__(self):
  • return self.callable(self.args, *self.kwargs)
  • try:
  • return self.callable(self.args, *self.kwargs)
  • except TypeError, x:
  • import re
  • if re.match(r'%s() takes .+ arguments? (.+ given)' %
  • re.escape(self.callable.name), x.args[0]):
  • raise cherrypy.HTTPError(404)
  • raise

class LateParamPageHandler(PageHandler): }}}

Comments (6)

  1. Robert Brewer reporter

    Here's a better one:

        def __call__(self):
                return self.callable(*self.args, **self.kwargs)
            except TypeError, x:
                funcname = re.escape(self.callable.__name__)
                msg = x.args[0]
                # TypeError: f() takes no arguments (1 given)
                # TypeError: f() takes exactly 1 argument (2 given)
                # TypeError: f() takes at most 1 argument (2 given)
                if re.match(r'^%s\(\) takes .+ arguments? \(.+ given\)$' %
                            funcname, msg):
                    raise cherrypy.HTTPError(404)
                # We do not want the following yet, because the behavior
                # should be: if the unexpected kwarg is a query param,
                # then 404, but if it's a body param, 400 Bad Request.
    ##            # TypeError: f() got multiple values for keyword argument 'a'
    ##            if re.match(r"^%s\(\) got an unexpected keyword argument '.+'$" %
    ##                        funcname, msg):
    ##                raise cherrypy.HTTPError(404)
                # This situation is even worse:
    ##            # TypeError: f() got an unexpected keyword argument 'b'

    Not sure what to do about the mixing of querystring and entity params...is half a solution better than none? or worse?

  2. Anonymous

    Added a new version that updates the documentation. It also refactors the method into the global name space so that it can be re-used in other dispatchers. The code has been marginally simplified in a few places.

  3. Log in to comment