Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #733 (defect)

Opened 10 months ago

Last modified 8 months ago

404 instead of 500 on wrong number of arguments

Status: assigned

Reported by: fumanchu Assigned to: fumanchu (accepted)
Priority: normal Milestone: 3.2
Component: CherryPy code Keywords:
Cc:

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):

Change History

09/21/07 11:23:56: Modified by fumanchu

  • status changed from new to assigned.

Here's a better one:

    def __call__(self):
        try:
            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'
            
            raise

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

10/26/07 00:45:34: Modified by fumanchu

  • milestone changed from 3.1 to 3.2.

Hosted by WebFaction

Log in as guest/cpguest to create tickets