我是Flask的新手,我正在尝试在python中显示Built-In Exceptions,但我似乎无法将它们展示在我的最后。
注意:
set FLASK_DEBUG = 0
码:
def do_something:
try:
doing_something()
except Exception as err:
return f"{err}"
期望:
现实:
也:
Flask
为您提供了一个功能,使您能够在整个app
中注册错误处理程序;你可以做一些如下所示的事情:
def handle_exceptions(e):
# Log exception in your logs
# get traceback and sys exception info and log as required
# app.logger.error(getattr(e, 'description', str(e)))
# Print traceback
# return your response using getattr(e, 'code', 500) etc.
# Exception is used to catch all exceptions
app.register_error_handler(Exception, handle_exceptions)
老实说,这是要走的路。 - 以werkzeug.exceptions.HTTPException
中的结构为例,是一个坚实的基础。
拥有一个统一的异常处理程序,可以使您的Exception
处理,可视化和日志记录标准化,这将使您的生活更加美好。 :)
试试这个:
def do_something:
try:
doing_something()
except Exception as err:
return f"{err.__class__.__name__}: {err}"