add template layer to error emails

This commit is contained in:
Lance Edgar 2012-11-06 08:00:40 -08:00
parent 4e7302a2eb
commit 99bde9e696
4 changed files with 72 additions and 9 deletions

View file

@ -19,3 +19,5 @@ recursive-include edbob/pyramid/templates *.mako
recursive-include edbob/scaffolds/edbob *.py recursive-include edbob/scaffolds/edbob *.py
recursive-include edbob/scaffolds/edbob *_tmpl recursive-include edbob/scaffolds/edbob *_tmpl
recursive-include edbob/scaffolds/edbob/+package+/pyramid/templates *.mako recursive-include edbob/scaffolds/edbob/+package+/pyramid/templates *.mako
recursive-include edbob/templates *.mako

View file

@ -26,6 +26,7 @@
``edbob.errors`` -- Error Alert Emails ``edbob.errors`` -- Error Alert Emails
""" """
import os.path
import sys import sys
import socket import socket
import logging import logging
@ -33,6 +34,7 @@ from traceback import format_exception
from cStringIO import StringIO from cStringIO import StringIO
import edbob import edbob
from edbob.files import resource_path
from edbob.mail import sendmail_with_config from edbob.mail import sendmail_with_config
@ -60,15 +62,51 @@ def email_exception(type=None, value=None, traceback=None):
if not (type and value and traceback): if not (type and value and traceback):
type, value, traceback = sys.exc_info() type, value, traceback = sys.exc_info()
body = StringIO()
hostname = socket.gethostname() hostname = socket.gethostname()
body.write("An exception occurred.\n") traceback = ''.join(format_exception(type, value, traceback))
body.write("\n") traceback = traceback.strip()
body.write("Machine Name: %s (%s)\n" % (hostname, socket.gethostbyname(hostname))) data = {
body.write("Local Time: %s\n" % (edbob.local_time().strftime('%Y-%m-%d %H:%M:%S %Z%z'))) 'host_name': hostname,
body.write("\n") 'host_ip': socket.gethostbyname(hostname),
body.write("%s\n" % ''.join(format_exception(type, value, traceback))) 'host_time': edbob.local_time(),
'traceback': traceback,
}
sendmail_with_config('errors', body.getvalue()) body, ctype = render_exception(data)
sendmail_with_config('errors', body, content_type=ctype)
def render_exception(data):
"""
Renders the exception data using a Mako template if one is configured;
otherwise as a simple string.
"""
template = edbob.config.get('edbob.errors', 'template')
if template:
template = resource_path(template)
if os.path.exists(template):
# Assume Mako template; render and return.
from mako.template import Template
template = Template(filename=template)
return template.render(**data), 'text/plain'
# If not a Mako template, return regular text with substitutions.
body = StringIO()
data['host_time'] = data['host_time'].strftime('%Y-%m-%d %H:%M:%S %Z%z')
body.write("""\
An unhandled exception occurred.
Machine Name: %(host_name)s (%(host_ip)s)
Machine Time: %(host_time)s
%(traceback)s
""" % data)
b = body.getvalue()
body.close() body.close()
return b, 'text/plain'

View file

@ -33,6 +33,8 @@ import shutil
import tempfile import tempfile
import lockfile import lockfile
import pkg_resources
__all__ = ['temp_path'] __all__ = ['temp_path']
@ -98,6 +100,18 @@ def count_lines(path):
return lines return lines
def resource_path(path):
"""
Returns a resource file path. ``path`` is assumed either to be a package
resource, or a regular file path. In the latter case it is returned
unchanged.
"""
if not os.path.isabs(path) and ':' in path:
return pkg_resources.resource_filename(*path.split(':'))
return path
def temp_path(suffix='.tmp', prefix='edbob.'): def temp_path(suffix='.tmp', prefix='edbob.'):
""" """
Convenience function to return a temporary file path. The arguments' Convenience function to return a temporary file path. The arguments'

View file

@ -0,0 +1,9 @@
An unhandled exception occurred.
*Machine Name:* ${host_name} (${host_ip})
*Machine Time:* ${host_time.strftime('%Y-%m-%d %H:%M:%S %Z%z')}
<pre>
${traceback}
</pre>