source: DataExtractor/trunk/dx-0.1.0/errorHandler.py @ 70

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DataExtractor/trunk/dx-0.1.0/errorHandler.py@70
Revision 70, 2.4 KB checked in by astephen, 16 years ago (diff)

Beta-release of dx: version 0.1.0

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1"""
2errorHandler.py
3===============
4
5Module to hold a suitable function for displaying errors and mailing
6the administrator when these occur.
7
8"""
9
10# import standard library modules
11import os, sys
12import smtplib
13
14# import local modules
15from shared_data import *
16from config import *
17
18class MailAdministrator:
19    """
20    MailAdministrator class - used to e-mail information to the administrator.
21    """
22   
23    def __init__(self, subject, adminEmail="a.stephens@rl.ac.uk"):
24        self.email=adminEmail
25        self.subject=subject
26        self.mailserver=smtplib.SMTP(LOCAL_MAIL_HOST)
27        self.mailserver.set_debuglevel(1)
28       
29    def sendMail(self, msg):
30        fromaddr=admin_mail_address
31        mail_content="""To: %s
32From: %s
33Subject: %s
34This message has been automatically generated by the %s\n
35%s""" % (self.email, fromaddr, self.subject, package_name, msg)
36
37        self.mailserver.sendmail(fromaddr, self.email, mail_content)
38        self.mailserver.quit()
39
40def displayError(msg, noheader=0):
41    """
42    displayError function - used to display a clean error to the user via
43    the main dx web interface page. The noheader argument tells the function
44    whether or not to display the full header before the error message.
45    """
46    if noheader==0:
47        print http_header
48        print " ".join(open(os.path.join(basedir, "html", "header.html")).readlines())
49    print "<P>An error has occurred with this application. The error message received is:<P>"
50    print "<P><B>%s</B>" % msg
51    print "<P>The administrator has been informed of this error and will look into it."
52    print """<P>You can try pressing the <I>Back</I> button to return to the previous page.
53Alternatively you can <A HREF="%s">restart your request</A>.""" % cgi_name
54    print " ".join(open(os.path.join(basedir, "html", "footer.html")).readlines())
55
56def handleError(msg, noheader=0, request_id=None, user=None):
57    """
58    handleError function - used to handle errors by calling displayError and
59    mailAdministrator functions.
60    """
61    # First report the error to the user
62    displayError(msg, noheader)
63    # Now mail administrator
64    stdout=sys.stdout
65    sys.stdout=RedirectStdout()
66    mailer=MailAdministrator("Error generated by %s: %s, %s" % (package_name, user, request_id))
67    appendee="""
68User: %s
69Request ID: %s
70""" % (user, request_id)
71    mailer.sendMail(msg+appendee)
72    sys.stdout=stdout
73    sys.exit()
Note: See TracBrowser for help on using the repository browser.