##// END OF EJS Templates
render custom HTML for error pages
MinRK -
Show More
@@ -24,7 +24,13 b' import os'
24 import stat
24 import stat
25 import sys
25 import sys
26 import traceback
26 import traceback
27 try:
28 # py3
29 from http.client import responses
30 except ImportError:
31 from httplib import responses
27
32
33 from jinja2 import TemplateNotFound
28 from tornado import web
34 from tornado import web
29
35
30 try:
36 try:
@@ -44,14 +50,7 b" UF_HIDDEN = getattr(stat, 'UF_HIDDEN', 32768)"
44 # Top-level handlers
50 # Top-level handlers
45 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
46
52
47 class RequestHandler(web.RequestHandler):
53 class AuthenticatedHandler(web.RequestHandler):
48 """RequestHandler with default variable setting."""
49
50 def render(*args, **kwargs):
51 kwargs.setdefault('message', '')
52 return web.RequestHandler.render(*args, **kwargs)
53
54 class AuthenticatedHandler(RequestHandler):
55 """A RequestHandler with an authenticated user."""
54 """A RequestHandler with an authenticated user."""
56
55
57 def clear_login_cookie(self):
56 def clear_login_cookie(self):
@@ -209,6 +208,45 b' class IPythonHandler(AuthenticatedHandler):'
209 raise web.HTTPError(400, u'Invalid JSON in body of request')
208 raise web.HTTPError(400, u'Invalid JSON in body of request')
210 return model
209 return model
211
210
211 def get_error_html(self, status_code, **kwargs):
212 """render custom error pages"""
213 exception = kwargs.get('exception')
214 message = ''
215 status_message = responses.get(status_code, 'Unknown')
216 if exception:
217 # get the custom message, if defined
218 try:
219 message = exception.log_message % exception.args
220 except Exception:
221 pass
222
223 # construct the custom reason, if defined
224 reason = getattr(exception, 'reason', '')
225 if reason:
226 status_message = reason
227
228 # build template namespace
229 ns = dict(
230 status_code=status_code,
231 status_message=status_message,
232 message=message,
233 exception=exception,
234 )
235
236 # render the template
237 try:
238 html = self.render_template('%s.html' % status_code, **ns)
239 except TemplateNotFound:
240 self.log.debug("No template for %d", status_code)
241 html = self.render_template('error.html', **ns)
242 return html
243
244
245 class Template404(IPythonHandler):
246 """Render our 404 template"""
247 def prepare(self):
248 raise web.HTTPError(404)
249
212
250
213 class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):
251 class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):
214 """static files should only be accessible when logged in"""
252 """static files should only be accessible when logged in"""
@@ -61,6 +61,7 b' from tornado import web'
61
61
62 # Our own libraries
62 # Our own libraries
63 from IPython.html import DEFAULT_STATIC_FILES_PATH
63 from IPython.html import DEFAULT_STATIC_FILES_PATH
64 from .base.handlers import Template404
64
65
65 from .services.kernels.kernelmanager import MappingKernelManager
66 from .services.kernels.kernelmanager import MappingKernelManager
66 from .services.notebooks.nbmanager import NotebookManager
67 from .services.notebooks.nbmanager import NotebookManager
@@ -208,6 +209,8 b' class NotebookWebApplication(web.Application):'
208 pattern = url_path_join(settings['base_project_url'], handler[0])
209 pattern = url_path_join(settings['base_project_url'], handler[0])
209 new_handler = tuple([pattern] + list(handler[1:]))
210 new_handler = tuple([pattern] + list(handler[1:]))
210 new_handlers.append(new_handler)
211 new_handlers.append(new_handler)
212 # add 404 on the end, which will catch everything that falls through
213 new_handlers.append((r'(.*)', Template404))
211 return new_handlers
214 return new_handlers
212
215
213
216
General Comments 0
You need to be logged in to leave comments. Login now