1 |
|
import traceback
|
|
1 |
|
2 |
2 |
import pprint
|
3 |
3 |
import sys
|
|
4 |
import traceback
|
|
5 |
import xtraceback
|
4 |
6 |
|
5 |
7 |
from cgi import escape
|
6 |
8 |
|
7 |
9 |
from mandaye.log import logger
|
8 |
10 |
from mandaye.http import HTTPResponse, HTTPHeader
|
9 |
|
from mandaye.config import debug, email_notification
|
|
11 |
from mandaye.config import debug, email_notification, use_long_trace
|
10 |
12 |
from mandaye.emails import Email
|
11 |
13 |
|
12 |
14 |
from mandaye.template import serve_template
|
... | ... | |
15 |
17 |
if email_notification:
|
16 |
18 |
email = Email()
|
17 |
19 |
|
18 |
|
def _get_html_traceback(tb_str, path, env=None):
|
|
20 |
def _get_html_error(tb_str, path, env=None):
|
|
21 |
""" Return a full html error with exception and environment
|
|
22 |
"""
|
19 |
23 |
msg = "<html><head><title>Mandaye traceback</title></head>"
|
20 |
|
msg += "<h2>%s</h2><h3>Exception</h3><pre>%s</pre>" % (path, tb_str)
|
|
24 |
msg += "<h2>%s</h2><h3>Exception</h3><pre>%s</pre>" % (path, escape(tb_str))
|
21 |
25 |
if env:
|
22 |
26 |
env = pprint.pformat(env)
|
23 |
27 |
msg += '<h3>Environment</h3><pre>%s</pre>' % escape(env)
|
24 |
28 |
msg += '</html>'
|
25 |
29 |
return msg
|
26 |
30 |
|
|
31 |
def _get_text_error(tbr_str, path, env=None):
|
|
32 |
""" Return a full text error with exception and environment
|
|
33 |
"""
|
|
34 |
error = "= Mandaye Traceback =\n\n"
|
|
35 |
error += "Path: %s\n\n== Exception ==\n" % path
|
|
36 |
error += tbr_str
|
|
37 |
error += "\n== Environment ==\n%s" % env
|
|
38 |
return error
|
|
39 |
|
|
40 |
def _get_traceback():
|
|
41 |
""" Return a string wih the traceback
|
|
42 |
"""
|
|
43 |
if use_long_trace:
|
|
44 |
tb_str = str(xtraceback.XTraceback(*sys.exc_info(), color=False))
|
|
45 |
else:
|
|
46 |
tb_str = traceback.format_exc()
|
|
47 |
return tb_str
|
|
48 |
|
27 |
49 |
def _302(location, cookies=None):
|
28 |
50 |
headers = HTTPHeader({'Location': [location],
|
29 |
51 |
'Content-Type': ['text/html']})
|
... | ... | |
51 |
73 |
path: the path of this error
|
52 |
74 |
exception: this exception instance if any """
|
53 |
75 |
if exception:
|
54 |
|
tb_str = traceback.format_exc()
|
|
76 |
tb_str = _get_traceback()
|
55 |
77 |
if email:
|
56 |
78 |
try:
|
57 |
79 |
email.sent('Mandaye internal server error',
|
58 |
|
_get_html_traceback(tb_str, path, env=env), 'html')
|
|
80 |
_get_text_error(tb_str, path, env=env))
|
59 |
81 |
except Exception as detail:
|
60 |
82 |
logger.warning('Sent mail failed with error: %s' % detail)
|
61 |
83 |
logger.exception("500 %s: %s, %s, env: %r" % (path, msg, tb_str, env))
|
... | ... | |
65 |
87 |
headers = HTTPHeader({'Content-Type': ['text/html']})
|
66 |
88 |
if debug:
|
67 |
89 |
if exception:
|
68 |
|
msg += _get_html_traceback(tb_str, path, env=env)
|
|
90 |
msg += _get_html_error(tb_str, path, env=env)
|
69 |
91 |
else:
|
70 |
92 |
msg=msg
|
71 |
93 |
else:
|
... | ... | |
87 |
109 |
and was unable to complete your request. Please contact the server \
|
88 |
110 |
administrator and inform them of the time the error occurred."
|
89 |
111 |
if debug and exception:
|
90 |
|
traceback.print_exc()
|
91 |
112 |
msg += "<h2>Target hostname: %s</h2>" % target_hostname
|
92 |
|
msg += _get_html_traceback(exception, path)
|
|
113 |
msg += _get_html_error(_get_traceback(), path)
|
93 |
114 |
return HTTPResponse(502, 'Bad Gateway', headers,
|
94 |
115 |
serve_template("response.html", title=title, body=msg))
|
95 |
116 |
|
Fix #718: manage long trace and send text email not html
xtraceback support