1 | # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) |
---|
2 | # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php |
---|
3 | |
---|
4 | """ |
---|
5 | WSGI middleware |
---|
6 | |
---|
7 | Captures any exceptions and prints a pretty report. See the `cgitb |
---|
8 | documentation <http://python.org/doc/current/lib/module-cgitb.html>`_ |
---|
9 | for more. |
---|
10 | """ |
---|
11 | |
---|
12 | import cgitb |
---|
13 | from cStringIO import StringIO |
---|
14 | import sys |
---|
15 | |
---|
16 | from paste.util import converters |
---|
17 | |
---|
18 | class NoDefault(object): |
---|
19 | pass |
---|
20 | |
---|
21 | class CgitbMiddleware(object): |
---|
22 | |
---|
23 | def __init__(self, app, |
---|
24 | global_conf=None, |
---|
25 | display=NoDefault, |
---|
26 | logdir=None, |
---|
27 | context=5, |
---|
28 | format="html"): |
---|
29 | self.app = app |
---|
30 | if global_conf is None: |
---|
31 | global_conf = {} |
---|
32 | if display is NoDefault: |
---|
33 | display = global_conf.get('debug') |
---|
34 | if isinstance(display, basestring): |
---|
35 | display = converters.asbool(display) |
---|
36 | self.display = display |
---|
37 | self.logdir = logdir |
---|
38 | self.context = int(context) |
---|
39 | self.format = format |
---|
40 | |
---|
41 | def __call__(self, environ, start_response): |
---|
42 | try: |
---|
43 | app_iter = self.app(environ, start_response) |
---|
44 | return self.catching_iter(app_iter, environ) |
---|
45 | except: |
---|
46 | exc_info = sys.exc_info() |
---|
47 | start_response('500 Internal Server Error', |
---|
48 | [('content-type', 'text/html')], |
---|
49 | exc_info) |
---|
50 | response = self.exception_handler(exc_info, environ) |
---|
51 | return [response] |
---|
52 | |
---|
53 | def catching_iter(self, app_iter, environ): |
---|
54 | if not app_iter: |
---|
55 | raise StopIteration |
---|
56 | error_on_close = False |
---|
57 | try: |
---|
58 | for v in app_iter: |
---|
59 | yield v |
---|
60 | if hasattr(app_iter, 'close'): |
---|
61 | error_on_close = True |
---|
62 | app_iter.close() |
---|
63 | except: |
---|
64 | response = self.exception_handler(sys.exc_info(), environ) |
---|
65 | if not error_on_close and hasattr(app_iter, 'close'): |
---|
66 | try: |
---|
67 | app_iter.close() |
---|
68 | except: |
---|
69 | close_response = self.exception_handler( |
---|
70 | sys.exc_info(), environ) |
---|
71 | response += ( |
---|
72 | '<hr noshade>Error in .close():<br>%s' |
---|
73 | % close_response) |
---|
74 | yield response |
---|
75 | |
---|
76 | def exception_handler(self, exc_info, environ): |
---|
77 | dummy_file = StringIO() |
---|
78 | hook = cgitb.Hook(file=dummy_file, |
---|
79 | display=self.display, |
---|
80 | logdir=self.logdir, |
---|
81 | context=self.context, |
---|
82 | format=self.format) |
---|
83 | hook(*exc_info) |
---|
84 | return dummy_file.getvalue() |
---|
85 | |
---|
86 | def make_cgitb_middleware(app, global_conf, |
---|
87 | display=NoDefault, |
---|
88 | logdir=None, |
---|
89 | context=5, |
---|
90 | format='html'): |
---|
91 | """ |
---|
92 | Wraps the application in the ``cgitb`` (standard library) |
---|
93 | error catcher. |
---|
94 | |
---|
95 | display: |
---|
96 | If true (or debug is set in the global configuration) |
---|
97 | then the traceback will be displayed in the browser |
---|
98 | |
---|
99 | logdir: |
---|
100 | Writes logs of all errors in that directory |
---|
101 | |
---|
102 | context: |
---|
103 | Number of lines of context to show around each line of |
---|
104 | source code |
---|
105 | """ |
---|
106 | from paste.deploy.converters import asbool |
---|
107 | if display is not NoDefault: |
---|
108 | display = asbool(display) |
---|
109 | if 'debug' in global_conf: |
---|
110 | global_conf['debug'] = asbool(global_conf['debug']) |
---|
111 | return CgitbMiddleware( |
---|
112 | app, global_conf=global_conf, |
---|
113 | display=display, |
---|
114 | logdir=logdir, |
---|
115 | context=context, |
---|
116 | format=format) |
---|