Author: ianb
Date: 2005-03-26 00:05:10 -0700 (Sat, 26 Mar 2005)
New Revision: 2232
Modified:
WSGIKit/trunk/wsgikit/error_middleware.py
WSGIKit/trunk/wsgikit/exceptions/reporter.py
Log:
Make the middleware more reliable when a reporter fails
Fix some email errors
Modified: WSGIKit/trunk/wsgikit/error_middleware.py
===================================================================
--- WSGIKit/trunk/wsgikit/error_middleware.py 2005-03-26 06:33:44 UTC (rev 2231)
+++ WSGIKit/trunk/wsgikit/error_middleware.py 2005-03-26 07:05:10 UTC (rev 2232)
@@ -1,4 +1,6 @@
import sys
+import traceback
+import cgi
try:
from cStringIO import StringIO
except ImportError:
@@ -54,7 +56,7 @@
return replacement_app
-def error_template(exception):
+def error_template(exception, extra):
return '''
<html>
<head>
@@ -63,35 +65,53 @@
<body>
<h1>Server Error</h1>
%s
+ %s
</body>
- </html>''' % exception
+ </html>''' % (exception, extra)
+def send_report(reporter, exc_data):
+ try:
+ reporter.report(exc_data)
+ except:
+ output = StringIO()
+ traceback.print_exc(file=output)
+ return """
+ <p>Additionally an error occurred while sending the %s report:
+
+ <pre>%s</pre>
+ </p>""" % (
+ cgi.escape(str(reporter)), output.getvalue())
+ else:
+ return ''
+
def exception_handler(exc_info, environ):
reported = False
exc_data = collector.collect_exception(*exc_info)
conf = environ['wsgikit.config']
+ extra_data = ''
if conf.get('error_email'):
rep = reporter.EmailReporter(
to_addresses=conf['error_email'],
from_address=conf.get('error_email_from', 'errors@...'),
smtp_server=conf.get('smtp_server', 'localhost'),
subject_prefix=conf.get('error_subject_prefix', ''))
- rep.report(exc_data)
+ extra_data += send_report(rep, exc_data)
reported = True
if conf.get('error_log'):
rep = reporter.LogReporter(
filename=conf['error_log'])
- rep.report(exc_data)
+ extra_data += send_report(rep, exc_data)
+ # Well, this isn't really true, is it?
reported = True
if conf.get('debug', False):
- html = error_template(formatter.format_html(exc_data))
+ html = error_template(formatter.format_html(exc_data), extra_data)
reported = True
else:
html = error_template(
'''
An error occurred. See the error logs for more information.
(Turn debug on to display exception reports here)
- ''')
+ ''', '')
if not reported:
stderr = environ['wsgi.errors']
err_report = formatter.format_text(exc_data, show_hidden_frames=True)
Modified: WSGIKit/trunk/wsgikit/exceptions/reporter.py
===================================================================
--- WSGIKit/trunk/wsgikit/exceptions/reporter.py 2005-03-26 06:33:44 UTC (rev 2231)
+++ WSGIKit/trunk/wsgikit/exceptions/reporter.py 2005-03-26 07:05:10 UTC (rev 2232)
@@ -27,7 +27,7 @@
def format_text(self, exc_data, **kw):
return formatter.format_text(exc_data, **kw)
-class EmailReport(Reporter):
+class EmailReporter(Reporter):
to_addresses = None
from_address = None
@@ -59,7 +59,7 @@
msg = MIMEMultipart()
msg.set_type('multipart/alternative')
msg.preamble = msg.epilogue = ''
- text_msg = MIMEText(test_version)
+ text_msg = MIMEText(text_version)
text_msg.set_type('text/plain')
text_msg.set_param('charset', 'ASCII')
msg.attach(text_msg)
@@ -70,11 +70,11 @@
html_long = MIMEText(long_html_version)
html_long.set_type('text/html')
html_long.set_param('charset', 'UTF-8')
- html_msg.attach(html_long)
msg.attach(html_msg)
+ msg.attach(html_long)
msg['Subject'] = '%s%s: %s' % (
- self.subject_prefix, self.exception_type,
- self.exception_value)
+ self.subject_prefix, exc_data.exception_type,
+ exc_data.exception_value)
msg['From'] = self.from_address
msg['To'] = ', '.join(self.to_addresses)
return msg
|