This repository has been archived by the owner on Oct 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
CGITB
76 lines (61 loc) · 2.03 KB
/
CGITB
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
You can obtain tracebacks in cgitb format by adding this simple Tool to your arsenal:
{{{
#!python
def err():
"""Replace the default error response with an HTML traceback from cgitb."""
import cgitb, sys
tb = cgitb.html(sys.exc_info())
def set_tb():
cherrypy.response.body = tb
cherrypy.response.headers['Content-Length'] = None
cherrypy.request.hooks.attach('after_error_response', set_tb)
cherrypy.tools.cgitb = cherrypy.Tool('before_error_response', err)
}}}
Enable it for any URI's in a config file:
{{{
[/]
tools.cgitb.on = True
}}}
...or for a class with {{{_cp_config}}}:
{{{
#!python
class Root:
_cp_config = {'tools.cgitb.on': True}
def default(self, *args, **kwargs):
return "args: %s kwargs: %s" % (args, kwargs)
default.exposed = True
}}}
----
Modifed version that will put details into your logs as well.
{{{
#!python
def cgitb_display_err():
"""Replace the default error response with an HTML traceback from cgitb."""
import cgitb, sys
tb = cgitb.html(sys.exc_info())
def set_tb():
cherrypy.response.body = tb
cherrypy.response.headers['Content-Length'] = None
cherrypy.request.hooks.attach('after_error_response', set_tb)
cherrypy.tools.cgitb_display_err = cherrypy.Tool('before_error_response', cgitb_display_err)
def cgitb_log_err():
"""Log cgitb details to the log."""
import cgitb, sys
tb = cgitb.text(sys.exc_info())
def set_tb():
cherrypy.log(tb)
cherrypy.request.hooks.attach('after_error_response', set_tb)
cherrypy.tools.cgitb_log_err = cherrypy.Tool('before_error_response', cgitb_log_err)
}}}
How to enable:
{{{
#!python
[/]
#Turn on cgitb displays:
tools.cgitb_display_err.on = True
#Turn on detailed logging (after turning off default logging):
tools.log_tracebacks.on = False
tools.cgitb_log_err.on = True
}}}
Here's an example of the output you'll get:
http://tools.cherrypy.org/attachment/wiki/CGITB/cgitb.jpg?format=raw