# HG changeset patch # User paulb # Date 1107041697 0 # Node ID 389274e79a9e144d0a85017de87b125de798f07b # Parent 3767617694d2ba2bce37c84775beb9f75bad41b5 [project @ 2005-01-29 23:34:57 by paulb] Added uncaught exception handling through the handle_errors parameter which is passed to adapters upon initialisation. diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/BaseHTTPRequestHandler.py --- a/WebStack/Adapters/BaseHTTPRequestHandler.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/BaseHTTPRequestHandler.py Sat Jan 29 23:34:57 2005 +0000 @@ -12,36 +12,41 @@ "A factory class creating WebStack dispatcher objects." - def __init__(self, resource, authenticator=None): + def __init__(self, resource, authenticator=None, handle_errors=1): """ Initialise the root application-specific 'resource' and optional - 'authenticator'. + 'authenticator'. The optional 'handle_errors' parameter (if true) causes + handlers to deal with uncaught exceptions cleanly. """ self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def __call__(self, request, client_address, server): "Act as a factory for the server objects." handler = Handler(request, client_address, server, self.webstack_resource, - self.webstack_authenticator) + self.webstack_authenticator, self.handle_errors) return handler class Handler(BaseHTTPServer.BaseHTTPRequestHandler): "A class dispatching requests to WebStack resources." - def __init__(self, request, client_address, server, resource, authenticator): + def __init__(self, request, client_address, server, resource, authenticator, handle_errors): """ Initialise the root application-specific 'resource' and 'authenticator'. + Where 'handle_errors' is true, uncaught exceptions are dealt with by the + handler. """ self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server) def handle(self): @@ -60,6 +65,11 @@ self.webstack_resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: trans.set_response_code(401) # Unauthorized trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/CGI.py --- a/WebStack/Adapters/CGI.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/CGI.py Sat Jan 29 23:34:57 2005 +0000 @@ -8,7 +8,7 @@ from WebStack.Generic import EndOfResponse import sys, os -def respond(resource, authenticator=None, input=None, output=None, env=None): +def respond(resource, authenticator=None, input=None, output=None, env=None, handle_errors=1): """ Dispatch to the root application-specific 'resource'. Employ the optional @@ -16,7 +16,8 @@ stream, 'output' stream or environment 'env' are specified, use them instead of the defaults: standard input, standard output and the operating system environment respectively. Note that 'env' must evaluate to true for it to - replace the default. + replace the default. The optional 'handle_errors' parameter (if true) causes + handlers to deal with uncaught exceptions cleanly. """ trans = WebStack.CGI.Transaction(input or sys.stdin, output or sys.stdout, @@ -28,6 +29,11 @@ resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: trans.set_response_code(401) # Unauthorized trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/JavaServlet.py --- a/WebStack/Adapters/JavaServlet.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/JavaServlet.py Sat Jan 29 23:34:57 2005 +0000 @@ -11,15 +11,17 @@ "A servlet helper class dispatching requests to WebStack resources." - def __init__(self, resource, authenticator=None): + def __init__(self, resource, authenticator=None, handle_errors=1): """ Initialise the root application-specific 'resource' and optional - 'authenticator'. + 'authenticator'. The optional 'handle_errors' parameter (if true) + causes handlers to deal with uncaught exceptions cleanly. """ self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def service(self, request, response): @@ -33,6 +35,11 @@ self.webstack_resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: trans.set_response_code(401) # Unauthorized #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/ModPython.py --- a/WebStack/Adapters/ModPython.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/ModPython.py Sat Jan 29 23:34:57 2005 +0000 @@ -8,12 +8,12 @@ from WebStack.Generic import EndOfResponse from mod_python import apache -def respond(request, resource, debug=0): +def respond(request, resource, handle_errors=1): """ Dispatch the given 'request' to the root application-specific 'resource'. - The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in - the browser. + The optional 'handle_errors' flag, if set to false, causes tracebacks to be + displayed in the browser. """ trans = WebStack.ModPython.Transaction(request) @@ -32,17 +32,17 @@ # NOTE: Error conditions should be investigated further, along with # NOTE: other response states. - if not debug: + if handle_errors: return apache.HTTP_INTERNAL_SERVER_ERROR else: raise -def authenticate(request, authenticator, debug=0): +def authenticate(request, authenticator, handle_errors=1): """ Dispatch the given 'request' to the application-specific 'authenticator'. - The optional 'debug' flag, if set to 1, causes tracebacks to be displayed in - the browser. + The optional 'handle_errors' flag, if set to false, causes tracebacks to be + displayed in the browser. """ trans = WebStack.ModPython.Transaction(request) @@ -60,7 +60,7 @@ # NOTE: Error conditions should be investigated further, along with # NOTE: other response states. - if not debug: + if handle_errors: return apache.HTTP_INTERNAL_SERVER_ERROR else: raise diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/Twisted.py --- a/WebStack/Adapters/Twisted.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/Twisted.py Sat Jan 29 23:34:57 2005 +0000 @@ -14,16 +14,18 @@ isLeaf = 1 - def __init__(self, resource, authenticator=None): + def __init__(self, resource, authenticator=None, handle_errors=1): """ Initialise the root application-specific 'resource' and optional - 'authenticator'. + 'authenticator'. The optional 'handle_errors' parameter (if true) causes + handlers to deal with uncaught exceptions cleanly. """ twisted.web.resource.Resource.__init__(self) self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def getChild(self, path, request): raise NotImplementedError, "getChild" @@ -38,6 +40,11 @@ self.webstack_resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: trans.set_response_code(401) # Unauthorized trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/WSGI.py --- a/WebStack/Adapters/WSGI.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/WSGI.py Sat Jan 29 23:34:57 2005 +0000 @@ -11,15 +11,17 @@ "A WSGI adapter class." - def __init__(self, resource, authenticator=None): + def __init__(self, resource, authenticator=None, handle_errors=1): """ Initialise the adapter with the given WebStack 'resource' and the - optional 'authenticator'. + optional 'authenticator'. The optional 'handle_errors' parameter (if + true) causes handlers to deal with uncaught exceptions cleanly. """ self.resource = resource self.authenticator = authenticator + self.handle_errors = handle_errors def __call__(self, environ, start_response): @@ -43,6 +45,11 @@ self.resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: trans.set_response_code(401) # Unauthorized trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/Webware.py --- a/WebStack/Adapters/Webware.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/Webware.py Sat Jan 29 23:34:57 2005 +0000 @@ -29,15 +29,18 @@ instantiation. """ - def __init__(self, resource, authenticator=None): + def __init__(self, resource, authenticator=None, handle_errors=1): """ Initialise the parser object with the given root application-specific - 'resource' and optional 'authenticator'. + 'resource' and optional 'authenticator'. The optional 'handle_errors' + parameter (if true) causes handlers to deal with uncaught exceptions + cleanly. """ self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def parse(self, trans, requestPath): @@ -52,7 +55,8 @@ should be available elsewhere. """ - return WebStackServlet(self.webstack_resource, self.webstack_authenticator) + return WebStackServlet(self.webstack_resource, self.webstack_authenticator, + self.handle_errors) # For Webware 0.8.1 and earlier, employ servlet factories and servlets. @@ -66,19 +70,22 @@ application-specific resources. """ - def __init__(self, application, resource, file_extensions, authenticator=None): + def __init__(self, application, resource, file_extensions, authenticator=None, handle_errors=1): """ Initialise the servlet factory with the Webware 'application' and the WebStack root application-specific 'resource'. The 'file_extensions' specified indicate for which files this factory is invoked. An optional - 'authenticator' is used to control access to the resource. + 'authenticator' is used to control access to the resource. The optional + 'handle_errors' parameter (if true) causes handlers to deal with + uncaught exceptions cleanly. """ ServletFactory.__init__(self, application) self.webstack_resource = resource self.file_extensions = file_extensions self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def uniqueness(self): @@ -105,7 +112,8 @@ method is invoked on the servlet. """ - return WebStackServlet(self.webstack_resource, self.webstack_authenticator) + return WebStackServlet(self.webstack_resource, self.webstack_authenticator, + self.handle_errors) # Servlets are common to both solutions. @@ -113,16 +121,18 @@ "A servlet which dispatches transactions to application-specific resources." - def __init__(self, resource, authenticator): + def __init__(self, resource, authenticator, handle_errors): """ Initialise the servlet with an application-specific 'resource' and - 'authenticator'. + 'authenticator'. Where 'handle_errors' is true, uncaught exceptions are + dealt with by the handler. """ Servlet.__init__(self) self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def respond(self, trans): @@ -137,6 +147,11 @@ self.webstack_resource.respond(new_trans) except EndOfResponse: pass + except: + if self.handle_errors: + new_trans.set_response_code(500) # Internal error + else: + raise else: new_trans.set_response_code(401) # Unauthorized new_trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( diff -r 3767617694d2 -r 389274e79a9e WebStack/Adapters/Zope.py --- a/WebStack/Adapters/Zope.py Fri Jun 20 22:27:50 2008 +0200 +++ b/WebStack/Adapters/Zope.py Sat Jan 29 23:34:57 2005 +0000 @@ -21,16 +21,18 @@ security.declareObjectProtected("View") security.declareProtected("View", "index_html") - def __init__(self, id, resource, authenticator=None): + def __init__(self, id, resource, authenticator=None, handle_errors=1): """ Initialise with an 'id', a WebStack 'resource', and an optional - 'authenticator'. + 'authenticator'. The optional 'handle_errors' parameter (if true) causes + handlers to deal with uncaught exceptions cleanly. """ self.id = id self.webstack_resource = resource self.webstack_authenticator = authenticator + self.handle_errors = handle_errors def __bobo_traverse__(self, request, entry_name): if entry_name == "index_html": @@ -56,6 +58,11 @@ self.webstack_resource.respond(trans) except EndOfResponse: pass + except: + if self.handle_errors: + trans.set_response_code(500) # Internal error + else: + raise else: #trans.set_header_value("WWW-Authenticate", '%s realm="%s"' % ( # self.webstack_authenticator.get_auth_type(), self.webstack_authenticator.get_realm()))