Show More
@@ -19,11 +19,14 b' Authors:' | |||
|
19 | 19 | |
|
20 | 20 | import datetime |
|
21 | 21 | import email.utils |
|
22 | import functools | |
|
22 | 23 | import hashlib |
|
24 | import json | |
|
23 | 25 | import logging |
|
24 | 26 | import mimetypes |
|
25 | 27 | import os |
|
26 | 28 | import stat |
|
29 | import sys | |
|
27 | 30 | import threading |
|
28 | 31 | |
|
29 | 32 | from tornado import web |
@@ -37,6 +40,7 b' except ImportError:' | |||
|
37 | 40 | from IPython.config import Application |
|
38 | 41 | from IPython.external.decorator import decorator |
|
39 | 42 | from IPython.utils.path import filefind |
|
43 | from IPython.utils.jsonutil import date_default | |
|
40 | 44 | |
|
41 | 45 | #----------------------------------------------------------------------------- |
|
42 | 46 | # Monkeypatch for Tornado <= 2.1.1 - Remove when no longer necessary! |
@@ -244,6 +248,7 b' class IPythonHandler(AuthenticatedHandler):' | |||
|
244 | 248 | use_less=self.use_less, |
|
245 | 249 | ) |
|
246 | 250 | |
|
251 | ||
|
247 | 252 | class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler): |
|
248 | 253 | """static files should only be accessible when logged in""" |
|
249 | 254 | |
@@ -252,6 +257,39 b' class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):' | |||
|
252 | 257 | return web.StaticFileHandler.get(self, path) |
|
253 | 258 | |
|
254 | 259 | |
|
260 | def json_errors(method): | |
|
261 | """Decorate methods with this to return GitHub style JSON errors. | |
|
262 | ||
|
263 | This should be used on any handler method that can raise HTTPErrors. | |
|
264 | ||
|
265 | This will grab the latest HTTPError exception using sys.exc_info | |
|
266 | and then: | |
|
267 | ||
|
268 | 1. Set the HTTP status code based on the HTTPError | |
|
269 | 2. Create and return a JSON body with a message field describing | |
|
270 | the error in a human readable form. | |
|
271 | """ | |
|
272 | @functools.wraps(method) | |
|
273 | def wrapper(self, *args, **kwargs): | |
|
274 | try: | |
|
275 | result = method(self, *args, **kwargs) | |
|
276 | except: | |
|
277 | t, value, tb = sys.exc_info() | |
|
278 | if isinstance(value, web.HTTPError): | |
|
279 | status = value.status_code | |
|
280 | message = value.log_message | |
|
281 | else: | |
|
282 | status = 400 | |
|
283 | message = u"Unknown server error" | |
|
284 | self.set_status(status) | |
|
285 | reply = dict(message=message) | |
|
286 | self.finish(json.dumps(reply, default=date_default)) | |
|
287 | else: | |
|
288 | return result | |
|
289 | return wrapper | |
|
290 | ||
|
291 | ||
|
292 | ||
|
255 | 293 | #----------------------------------------------------------------------------- |
|
256 | 294 | # File handler |
|
257 | 295 | #----------------------------------------------------------------------------- |
General Comments 0
You need to be logged in to leave comments.
Login now