move wsgi.Application error handling to handle_error method
authorTero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 01:40:45 +0200
changeset 72 5160b9e0edf1
parent 71 0162c2b21dc5
child 73 1554d3d083b8
move wsgi.Application error handling to handle_error method
wsgi.py
--- a/wsgi.py	Mon Feb 16 01:40:26 2009 +0200
+++ b/wsgi.py	Mon Feb 16 01:40:45 2009 +0200
@@ -46,6 +46,23 @@
         # send respones data
         yield response.get_data()
 
+    def handle_error (self, exc_info, env, start_response) :
+        """
+            Handle errors thrown by handle_request. This should call start_response with the exc_info and return the
+            error message (i.e. str), otherwise the error will be thrown up yet another level.
+
+            Note that e.g. unicode is a failure...
+        """
+
+        # try and send 500 ISE to browser, if no headers yet...
+        start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=UTF-8")], exc_info)
+
+        # format traceback
+        data = ''.join(traceback.format_exception(*exc_info))
+        
+        # no unicode, kplzthx
+        return data.encode('utf-8')
+    
     def __call__ (self, env, start_response) :
         """
             Wraps handle_request to trap errors
@@ -60,9 +77,6 @@
             # execption info
             info = sys.exc_info()
 
-            # try and send 500 ISE to browser, if no headers yet...
-            start_response("500 Internal Server Error", [('Content-type', "text/plain; charset=utf8")], info)
+            # handle
+            yield self.handle_error(info, env, start_response)
 
-            # send traceback
-            yield traceback.format_exc()
-