source: TI03-DataExtractor/branches/old_stuff/dx/errorHandler.py @ 793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI03-DataExtractor/branches/old_stuff/dx/errorHandler.py@793
Revision 793, 2.9 KB checked in by astephen, 13 years ago (diff)

Put all the old code in the old_stuff branch.

  • Property svn:executable set to *
Line 
1"""
2errorHandler.py
3===============
4
5Module to hold a suitable classes 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 config import *
16from common import *
17
18class MailAdministrator:
19    """
20    MailAdministrator class - used to e-mail information to the administrator.
21    """
22   
23    def __init__(self, subject, adminEmail=ADMIN_MAIL_ADDRESS):
24        """
25        __init__ method - takes a subject and administrator's e-mail address and
26        constructs instance variables to be used in the mailing.
27        """
28        self.email=adminEmail
29        self.subject=subject
30        self.mailserver=smtplib.SMTP(LOCAL_MAIL_HOST)
31        self.mailserver.set_debuglevel(1)
32       
33    def sendMail(self, msg):
34        """
35        sendMail method - actually send out the e-mail to the administrator
36        with the message given in the msg argument.
37        """
38        fromaddr=ADMIN_MAIL_ADDRESS
39        mail_content="""To: %s
40From: %s
41Subject: %s
42This message has been automatically generated by the %s\n
43%s""" % (self.email, fromaddr, self.subject, PACKAGE_NAME, msg)
44
45        self.mailserver.sendmail(fromaddr, self.email, mail_content)
46        self.mailserver.quit()
47
48
49class ErrorHandler:
50    """
51    ErrorHandler class - used to handle errors sensibly by reporting on-screen
52    to the user and sending a message to the administrator.
53    """
54   
55    def __init__(self, msg, noheader=0, request_id=None, user=None):
56        """
57        Method to build the instance variables required in the class and
58        calls the appropriate internal methods. Used to handle errors by calling displayError and
59        MailAdministrator class.
60        """
61
62        # First report the error to the user
63        self.displayError(msg, noheader)
64
65        stdout=sys.stdout
66        sys.stdout=RedirectStdout()
67
68        # Mail the administrator if switched on (1)       
69        if ADMIN_NOTIFICATION==1:
70            mailer=MailAdministrator("Error generated by %s: %s, %s" % (PACKAGE_NAME, user, request_id))
71            appendee="""
72        User: %s
73        Request ID: %s
74        """ % (user, request_id)
75            mailer.sendMail(msg+appendee)
76
77        sys.stdout=stdout
78        sys.exit()
79
80   
81    def displayError(self, msg, noheader=0):
82        """
83        displayError method - used to display a clean error to the user via
84        the main dx web interface page. The noheader argument tells the function
85        whether or not to display the full header before the error message.
86        """
87        if noheader==0:
88                print HTTP_HEADER
89                print " ".join(open(os.path.join(BASEDIR, "html", "header.html")).readlines())
90        print "<P>An error has occurred with this application. The error message received is:<P>"
91        print "<P><B>%s</B>" % msg
92        print "<P>The administrator has been informed of this error and will look into it."
93        print """<P>You can try pressing the <I>Back</I> button to return to the previous page.
94        Alternatively you can <A HREF="%s">restart your request</A>.""" % CGI_NAME
95        print " ".join(open(os.path.join(BASEDIR, "html", "footer.html")).readlines())
96   
Note: See TracBrowser for help on using the repository browser.