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

Changeset 884

Show
Ignore:
Timestamp:
12/27/05 18:20:54
Author:
fumanchu
Message:

Fix for #186 (allow exceptions to escape CP and up into WSGI middleware).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cphttpserver.py

    r873 r884  
    66import socket 
    77import SocketServer 
     8import sys 
    89import threading 
    910import time 
     
    9091            tb = _cputil.formatExc() 
    9192            cherrypy.log(tb) 
    92             if not cherrypy.config.get("server.show_tracebacks", False): 
    93                 tb = "" 
    94             s, h, b = _cputil.bareError(tb) 
     93            if cherrypy.config.get("server.throw_errors", False): 
     94                msg = "THROWN ERROR: %s" % sys.exc_info()[0].__name__ 
     95                s = "500 Internal Server Error" 
     96                h = [('Content-Type', 'text/plain'), 
     97                     ('Content-Length', str(len(msg)))] 
     98                b = [msg] 
     99            else: 
     100                if not cherrypy.config.get("server.show_tracebacks", False): 
     101                    tb = "" 
     102                s, h, b = _cputil.bareError(tb) 
    95103         
    96104        try: 
  • trunk/cherrypy/_cphttptools.py

    r879 r884  
    115115            raise 
    116116        except: 
     117            if cherrypy.config.get("server.throw_errors", False): 
     118                raise 
    117119            cherrypy.response.handleError(sys.exc_info()) 
    118120         
  • trunk/cherrypy/_cpwsgi.py

    r856 r884  
    7878        raise 
    7979    except: 
     80        if cherrypy.config.get("server.throw_errors", False): 
     81            raise 
    8082        tb = _cputil.formatExc() 
    8183        cherrypy.log(tb) 
     
    170172    RequestHandlerClass = CPHTTPRequest 
    171173     
    172     def __init__(self): 
     174    def __init__(self, app=wsgiApp): 
    173175        conf = cherrypy.config.get 
    174176         
     
    180182         
    181183        s = _cpwsgiserver.CherryPyWSGIServer 
    182         s.__init__(self, bind_addr, wsgiApp, 
     184        s.__init__(self, bind_addr, app, 
    183185                   conf("server.thread_pool"), 
    184186                   conf("server.socket_host"), 
  • trunk/cherrypy/test/helper.py

    r877 r884  
    2929 
    3030import cherrypy 
     31from cherrypy import _cpwsgi 
    3132import webtest 
    3233 
     
    5657##vroot = "/vpath" 
    5758test_vrf = VirtualRootFilter(vroot) 
     59 
     60 
     61def error_middleware(environ, start_response): 
     62    started = [False] 
     63    def start(s, h, exc=None): 
     64        started[0] = True 
     65        start_response(s, h, exc) 
     66     
     67    try: 
     68        for chunk in _cpwsgi.wsgiApp(environ, start): 
     69            yield chunk 
     70    except (KeyboardInterrupt, SystemExit): 
     71        raise 
     72    except Exception, x: 
     73        # We should only reach this point if server.throw_errors is True. 
     74        if not started[0]: 
     75            start_response("500 Server Error", []) 
     76        yield "THROWN ERROR: %s" % x.__class__.__name__ 
     77 
     78 
     79class TestWSGI(_cpwsgi.WSGIServer): 
     80    """Wrapper for WSGI server so we can test thrown errors.""" 
     81     
     82    def __init__(self): 
     83        _cpwsgi.WSGIServer.__init__(self, error_middleware) 
    5884 
    5985 
  • trunk/cherrypy/test/test.py

    r883 r884  
    2626                                    "cherrypy._cphttpserver.embedded_server"), 
    2727                         'wsgi': (2, "Native WSGI Server", 
    28                                   "cherrypy._cpwsgi.WSGIServer"), 
     28                                  "cherrypy.test.helper.TestWSGI"), 
    2929                         } 
    3030    default_server = "wsgi" 
  • trunk/cherrypy/test/test_core.py

    r879 r884  
    192192    def log_unhandled(self): 
    193193        raise ValueError() 
     194     
     195    def rethrow(self): 
     196        """Test that an error raised here will be thrown out to the server.""" 
     197        raise ValueError() 
    194198 
    195199 
     
    392396        'server.log_tracebacks': False, 
    393397        'server.log_unhandled_tracebacks': True, 
     398    }, 
     399    '/error/rethrow': { 
     400        'server.throw_errors': True, 
    394401    }, 
    395402    '/cpfilterlist/errinstream': { 
     
    691698               "[Errno 2] No such file or directory: 'nonexistent.html'") 
    692699        self.assertInBody(msg) 
     700         
     701        # Test server.throw_errors (ticket #186). 
     702        httpcls = cherrypy.server.httpserverclass 
     703        if httpcls: 
     704            self.getPage("/error/rethrow") 
     705            self.assertBody("THROWN ERROR: ValueError") 
     706        else: 
     707            self.assertRaises(ValueError, self.getPage, "/error/rethrow") 
    693708     
    694709    def testRanges(self): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets