-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.py
More file actions
85 lines (61 loc) · 2.12 KB
/
errors.py
File metadata and controls
85 lines (61 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import functools
import smtplib
import email
import traceback
from hashlib import md5
from logging import debug, info, warning
import tornado.web
"""
Uncaught exceptions are sent via email to the list of RECIPIENTS below,
with a hash of the traceback to help gmail keep threads straight.
IMPORTANT: debug mode (--debug=True from the command line) turns this off!
"""
RECIPIENTS = ['devops@edgeflip.com',]
def mail_tracebacks(method):
"""
Decorate arbitrary methods to forward tracebacks on to concerned parties
"""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
#check for debug mode
try:
return method(self, *args, **kwargs)
except:
# if debug mode is off, email the stack trace
from tornado.options import options
if options.debug: raise
else: traceback.print_exc()
err = traceback.format_exc()
msg = email.Message.Message()
msg['Subject'] = 'UNHANDLED EXCEPTION {}'.format(md5(err).hexdigest())
msg.set_payload(err)
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('syncerror@edgeflip.com', RECIPIENTS, msg.as_string())
return wrapper
class ErrorHandler(object):
"""
Mixin to tornado.request.handler to email off tracebacks for uncaught exceptions
"""
def send_error(self, status_code=500, **kwargs):
from tornado.options import options
if not options.debug:
err = traceback.format_exc()
payload = """
REQUEST
{}
TRACEBACK
{}
""".format( self.request, err)
msg = email.Message.Message()
msg['Subject'] = 'UNHANDLED EXCEPTION {}'.format(md5(err).hexdigest())
msg.set_payload(payload)
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('dashboarderror@edgeflip.com', RECIPIENTS, msg.as_string())
super(ErrorHandler, self).send_error()
class Errorer(ErrorHandler, tornado.web.RequestHandler):
def get(self):
raise Exception
def post(self):
raise Exception