【发布时间】:2019-03-22 23:31:01
【问题描述】:
我有以下错误处理程序:
@api.errorhandler(DatabaseException)
def handle_database_exception(database_error):
return database_error.query_error, 400
@api.errorhandler(ValidationException)
def handle_validation_exception(validation_error):
return {'fields': validation_error.body_error}, 400
这些是简单的类:
class ValidationException(Exception):
def __init__(self, body_error=None, missing_files=None):
Exception.__init__(self, "Validation failed")
self.body_error = body_error
if missing_files is not None:
for missing_file in missing_files:
self.body_error[missing_file] = ['required file']
class DatabaseException(Exception):
def __init__(self, details):
Exception.__init__(self, "Database Error")
self.query_error = details
这是我的问题: 如果我在我的任何路由中调用 raise DatabaseException,它就会失败,我会从烧瓶中得到一个 500 模板。
真正有趣的是,之前实现的 ValidationException 运行良好。
我详细了解了发生了什么,当引发 ValidationException 时,它会通过 response.py 并最终进入错误处理程序。不幸的是,我无法理解烧瓶深处发生的一切,但在调试中,DatabaseException 肯定走的是不同的路线。
我希望错误处理程序被调用。如果我在其中一条路由中引发 DatabaseException,它应该被调用。
【问题讨论】:
标签: python flask flask-restful