1.1 --- a/WebStack/Adapters/WSGI.py Thu Nov 16 23:40:10 2006 +0000
1.2 +++ b/WebStack/Adapters/WSGI.py Thu Nov 16 23:40:31 2006 +0000
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 WSGI adapter.
1.6
1.7 -Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2004, 2005, 2006 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This library is free software; you can redistribute it and/or
1.11 modify it under the terms of the GNU Lesser General Public
1.12 @@ -23,22 +23,26 @@
1.13 import WebStack.WSGI
1.14 from WebStack.Generic import EndOfResponse
1.15 from WebStack.Adapters.Helpers.wsgi_cgi import run_with_cgi
1.16 +from WebStack.Adapters.Helpers.Error import ErrorResource
1.17
1.18 class WSGIAdapter:
1.19
1.20 "A WSGI adapter class."
1.21
1.22 - def __init__(self, resource, authenticator=None, handle_errors=1):
1.23 + def __init__(self, resource, authenticator=None, handle_errors=1, error_resource=None):
1.24
1.25 """
1.26 Initialise the adapter with the given WebStack 'resource' and the
1.27 optional 'authenticator'. The optional 'handle_errors' parameter (if
1.28 - true) causes handlers to deal with uncaught exceptions cleanly.
1.29 + true) causes handlers to deal with uncaught exceptions cleanly, and the
1.30 + optional 'error_resource' specifies an alternative error message
1.31 + generation resource.
1.32 """
1.33
1.34 self.resource = resource
1.35 self.authenticator = authenticator
1.36 self.handle_errors = handle_errors
1.37 + self.error_resource = error_resource or ErrorResource()
1.38
1.39 def __call__(self, environ, start_response):
1.40
1.41 @@ -64,7 +68,9 @@
1.42 pass
1.43 except:
1.44 if self.handle_errors:
1.45 + trans.rollback()
1.46 trans.set_response_code(500) # Internal error
1.47 + self.error_resource.respond(trans)
1.48 else:
1.49 raise
1.50 else:
1.51 @@ -83,7 +89,7 @@
1.52 )
1.53 return [trans.get_wsgi_content()]
1.54
1.55 -def deploy(resource, authenticator=None, address=None, handle_errors=1):
1.56 +def deploy(resource, authenticator=None, address=None, handle_errors=1, error_resource=None):
1.57
1.58 """
1.59 Deploy the given 'resource', with the given optional 'authenticator', at the
1.60 @@ -93,10 +99,11 @@
1.61 NOTE: The 'address' is ignored with the current WSGI implementation.
1.62
1.63 The optional 'handle_errors' flag (true by default) specifies whether error
1.64 - conditions are handled gracefully.
1.65 + conditions are handled gracefully, and the optional 'error_resource'
1.66 + specifies an alternative error message generation resource, if desired.
1.67 """
1.68
1.69 - handler = WSGIAdapter(resource, authenticator, handle_errors)
1.70 + handler = WSGIAdapter(resource, authenticator, handle_errors, error_resource)
1.71 run_with_cgi(handler)
1.72
1.73 # vim: tabstop=4 expandtab shiftwidth=4