Search code examples
pythondebuggingcherrypywerkzeug

CherryPy + Werkzeug Debugger?


I was comparing CherryPy and Flask when I ran into the Werkzeug Debugger, which I really like. What wonders me:

Is it possible to integrate Werkzeug's debugger into CherryPy? If so: how?

When I tried to integrate it myself, I got the console working (/console), but not the exception handler.

EDIT: Seems like CherryPy catches the errors and handles them, before Werkzeug gets them.


Solution

  • In my edit I described that CherryPy catches the errors. In the config throw_errors can be set to True. For me, setting cherrypy._cprequest.Request.throw_errors = True did this. The whole code is:

    import cherrypy
    from cherrypy import wsgiserver
    
    from werkzeug.debug import DebuggedApplication
    
    class Root(object):
    
        @cherrypy.expose
        def index(self):
            return "Hello World :)"
    
        @cherrypy.expose
        def page(self):
            # Error:
            return self.self.self.pas
    
    cherrypy._cprequest.Request.throw_errors = True
    
    app = cherrypy.Application(Root(), script_name=None, config=None)
    app = DebuggedApplication(app, evalex=True)
    
    d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
    server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), d)
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
    

    I'm sure, there are better ways to do it, but I'm a newbie concerning CherryPy and this hack worked for me.