1 | """ |
---|
2 | errorHandler.py |
---|
3 | =============== |
---|
4 | |
---|
5 | Module to hold a suitable function for dealing with errors. |
---|
6 | Display errors and/or mailing the administrator when these occur. |
---|
7 | Do different things if running batch script or interactively. |
---|
8 | |
---|
9 | """ |
---|
10 | |
---|
11 | # import standard library modules |
---|
12 | import os, sys |
---|
13 | import smtplib |
---|
14 | |
---|
15 | #class MailAdministrator: |
---|
16 | # """ |
---|
17 | # MailAdministrator class - used to e-mail information to the administrator. |
---|
18 | # """ |
---|
19 | # |
---|
20 | # def __init__(self, subject, adminEmail="s.e.latham@rl.ac.uk"): |
---|
21 | # self.email=adminEmail |
---|
22 | # self.subject=subject |
---|
23 | # self.mailserver=smtplib.SMTP(LOCAL_MAIL_HOST) |
---|
24 | # self.mailserver.set_debuglevel(1) |
---|
25 | # |
---|
26 | # def sendMail(self, msg): |
---|
27 | # fromaddr=admin_mail_address |
---|
28 | # mail_content="""To: %s |
---|
29 | # From: %s |
---|
30 | # Subject: %s |
---|
31 | # This message has been automatically generated by the %s\n |
---|
32 | # %s""" % (self.email, fromaddr, self.subject, package_name, msg) |
---|
33 | # |
---|
34 | # self.mailserver.sendmail(fromaddr, self.email, mail_content) |
---|
35 | # self.mailserver.quit() |
---|
36 | |
---|
37 | class handleError: |
---|
38 | def __init__(self,run_mode='batch'): |
---|
39 | self.run_mode = run_mode |
---|
40 | |
---|
41 | def displayError(self): |
---|
42 | """ |
---|
43 | displayError function - used to display a clean error to the user via |
---|
44 | the applications web interface page. The noheader argument tells the function |
---|
45 | whether or not to display the full header before the error message. |
---|
46 | """ |
---|
47 | if self.run_mode == 'batch': |
---|
48 | print "An error has occurred within this application." |
---|
49 | print "The error message received is: %s" % self.msg |
---|
50 | |
---|
51 | # if noheader==0: |
---|
52 | # print http_header |
---|
53 | # print " ".join(open(os.path.join(basedir, "html", "header.html")).readlines()) |
---|
54 | # print "<P>An error has occurred with this application. The error message received is:<P>" |
---|
55 | # print "<P><B>%s</B>" % msg |
---|
56 | # print "<P>The administrator has been informed of this error and will look into it." |
---|
57 | # print """<P>You can try pressing the <I>Back</I> button to return to the previous page. |
---|
58 | # Alternatively you can <A HREF="%s">restart your request</A>.""" % cgi_name |
---|
59 | # print " ".join(open(os.path.join(basedir, "html", "footer.html")).readlines()) |
---|
60 | |
---|
61 | def handleMiscError(self,msg,fatal=False): |
---|
62 | """ |
---|
63 | handleError function - used to handle errors by calling displayError and/or |
---|
64 | mailAdministrator functions as required by run_mode. |
---|
65 | """ |
---|
66 | self.fatal = fatal |
---|
67 | self.msg = msg |
---|
68 | if self.run_mode=='batch': |
---|
69 | self.displayError() |
---|
70 | if self.fatal: |
---|
71 | print "Fatal error - application aborted" |
---|
72 | sys.exit() |
---|
73 | |
---|
74 | # # First report the error to the user |
---|
75 | # displayError(msg, noheader) |
---|
76 | # # Now mail administrator |
---|
77 | # stdout=sys.stdout |
---|
78 | # sys.stdout=RedirectStdout() |
---|
79 | # mailer=MailAdministrator("Error generated by %s" % package_name) |
---|
80 | # mailer.sendMail(msg) |
---|
81 | # stdout=sys.stdout |
---|
82 | # sys.stdout=RedirectStdout() |
---|
83 | # mailer=MailAdministrator("Error generated by %s" % package_name) |
---|
84 | # mailer.sendMail(msg) |
---|
85 | # sys.stdout=stdout |
---|
86 | # sys.exit() |
---|