durusmail: quixote-users: A better patch (Re: Exception-handling patch for medusa_http.py)
Exception-handling patch for medusa_http.py
2003-01-17
A better patch (Re: Exception-handling patch for medusa_http.py)
2003-01-17
A better patch (Re: Exception-handling patch for medusa_http.py)
2003-01-20
Re: A better patch (Re: Exception-handling patch for medusa_http.py)
2003-01-20
2003-01-17
A better patch (Re: Exception-handling patch for medusa_http.py)
Graham Fawcett
2003-01-17
I'd like to suggest this patch as an alternative to the last one I
posted. I didn't realize that other exceptions (such as those caused in
PTL templates) are not wrapped in PublishErrors.

This patch catches all exceptions from the Publisher, and wraps
non-PublishError exceptions in a medusa_http.ServerError instance.

-- Graham

--- C:\projects\Quixote-0.6b1\server\medusa_http.py     Fri Jan 17 00:42:48 2003
+++ C:\Python22\Lib\site-packages\quixote\server\medusa_http.py Fri Jan
17 13:44:52 2003
@@ -15,4 +15,22 @@
  from quixote.http_request import HTTPRequest
  from quixote.publish import Publisher
+from quixote.errors import PublishError
+import sys
+import traceback
+
+class ServerError (PublishError):
+    """
+    Raised when a non-PublishError exception is raised in the publisher.
+    """
+    status_code = 500
+    title = "Server Error"
+    description = "Server Error"
+
+    def __init__ (self):
+        e1,e2,e3 = sys.exc_info()
+        public_msg = 'An error occured in the application.'
+        private_msg = ''.join(traceback.format_exception(e1,e2,e3))
+        PublishError.__init__(self, public_msg, private_msg)
+        print private_msg

  class QuixoteHandler:
@@ -52,4 +70,6 @@
                     'HTTP_COOKIE':msg.get('Cookie'),
                     'HTTP_REFERER':msg.get('Referer'),
+                   'ACCEPT_ENCODING':msg.get('Accept-encoding'),
+                   'HTTP_USER_AGENT':msg.get('user-agent'),
                     'PATH_INFO':request.uri,
                     'QUERY_STRING':query_string,
@@ -63,14 +83,19 @@
                     'SERVER_SOFTWARE':self.server_name,
                     }
-       for k,v in environ.items():
-           if v == None:
-               environ[k] = ''
+        for k,v in environ.items():
+            if v == None:
+                environ[k] = ''

          stdin = StringIO(data)
          qreq = HTTPRequest(stdin, environ)
          qreq.process_inputs()
-        output = self.publisher.process_request(qreq, environ)
+        try:
+            output = self.publisher.process_request(qreq, environ)
+        except Exception, err:
+            if not isinstance(err, PublishError):
+                err = ServerError()
+            output = self.publisher.finish_interrupted_request(qreq, err)
          if output:
-            qreq.response.set_body(output)
+            qreq.response.set_body(str(output))

          output_file = StringIO()



reply