python-3.x - Python 捕获 Tornado Web 服务器中的所有错误
问题描述
我将 Tornado 作为异步应用程序服务器(不是 WSGI)运行。我需要捕获所有已定义的 api 端点/Tornado 处理程序的所有服务器错误(如500 Internal Server Error), so I added this
ErrorHandler` 类(根据此处)
#!/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)
但它不起作用。当调用导致 500 错误的/data
端点时,customHandler
我仍然得到一个
<html>
<title>500: Internal Server Error</title>
<body>500: Internal Server Error</body>
</html>
解决方案
推荐阅读
- firebase - 没有 Cocoa Pod 的 tvOS 的 Firebase
- reactjs - 将 Jest 与 Bash 一起使用时,交互式命令不起作用
- google-domains - 在 google 域中创建通配符子域
- android - android颤动中带有gif图像的启动屏幕
- c++ - 函数中的 switch case 语句不会切换 C++
- firebase - 如果有人窃取了您的 Firebase 身份验证刷新令牌,会发生什么情况?
- javascript - 如何使用 TypeScript 最好地描述这段代码?
- python - 如何允许从所有目录访问文件?
- r - 减少 Rmarkdown (ioslides) 中 kable 表中的列标题名称字体大小
- c# - 仅当它具有非空值时,如何将可为空的日期时间转换为日期时间