Skip to content

Instantly share code, notes, and snippets.

@loretoparisi
Created October 24, 2019 10:36
Show Gist options
  • Save loretoparisi/ad24c109af5c4f3f2f36606ec6e06008 to your computer and use it in GitHub Desktop.
Save loretoparisi/ad24c109af5c4f3f2f36606ec6e06008 to your computer and use it in GitHub Desktop.
Tornado Catch allError Handler example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import handlers as th
from tornado import web
class ErrorHandler(web.RequestHandler):
"""Generates an error response with status_code for all requests."""
def write_error(self, status_code, **kwargs):
print('In get_error_html. status_code: ', status_code)
if status_code in [403, 404, 500, 503]:
self.write('Error %s' % status_code)
else:
self.write('BOOM!')
def prepare(self):
print('In prepare...')
raise Exception('Error!')
def application():
handlers = [
# Catch all Error handler
(r"/", ErrorHandler),
(r'/data', th.customHandler)
]
settings = dict()
return web.Application(handlers, **settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment