Skip to content

TypeError swallowed in decorated handlers #1530

@lbolla

Description

@lbolla

If a TypeError is raised within a handler, which has been decorated by a "wrapper" function whose first argument is not called self, the TypeError is swallowed and a 404 HTTP Error is returned instead.

from functools import wraps
import cherrypy


def dec(f):
    @wraps(f)
    def wrapper(handler, *args, **kwargs):
        return f(handler, *args, **kwargs)
    return wrapper


class HelloWorld(object):

    @dec
    def index(self, *args, **kwargs):
        raise TypeError('OOPS')
        return "Hello world!"

    index.exposed = True


if __name__ == '__main__':
    cherrypy.quickstart(HelloWorld())

Response is a 404:

> curl -sD - 'http://localhost:8080/' -o /dev/null
HTTP/1.1 404 Not Found
Server: CherryPy/8.1.2
Content-Length: 1267
Content-Type: text/html;charset=utf-8
Date: Fri, 16 Dec 2016 16:05:18 GMT

Without the decorator @dec, one correctly gets a 500.

The problem is here: https://github.com/cherrypy/cherrypy/blob/master/cherrypy/_cpdispatch.py#L105
When inspecting the "callable", CherryPy checks for the name of the first argument and strips it out if it's called self. It then goes on inspecting the other arguments and raises a 404 if some arguments are missing or unknown.
But if the "callable"'s first argument is not called "self", as in the decorated handler above (where it's called "handler"), then CherryPy mistakenly returns a "404 argument 'handler' is unknown".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions