Created
October 24, 2019 10:36
-
-
Save loretoparisi/ad24c109af5c4f3f2f36606ec6e06008 to your computer and use it in GitHub Desktop.
Tornado Catch allError Handler example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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