python - 烧瓶如何在使用 @app.errorhandler(Exception) 时不显示错误页面但继续应用程序流程
问题描述
我有
@app.errorhandler(Exception)
def unhandled(error):
print(error)
etype, value, tb = sys.exc_info()
print(traceback.print_exception(etype, value, tb))
logger.error("Exception %s" % traceback.format_exc())
logger.error("Exception %s" % traceback.print_exception(etype, value, tb))
logger.info("Exception %s" % traceback.format_exc())
logger.info("Exception %s" % traceback.print_exception(etype, value, tb))
print(traceback.format_exc())
return None
@app.route('/')
def index():
logger.info("A %s- B: %s" % project_dict) # raise exception
a = 1
b = 2
c = 3
问题是我不使用这个函数来打破调用处理程序@app.route('/') 的流程只是为了打印到日志并继续
a = 1
b = 2
c = 3
解决方案
您如何看待将可能的错误执行集成到 try-except 块中?
# This code is never executed due to an error that occurs within
# the try-except block in the route `index`.
@app.errorhandler(Exception)
def unhandled(error):
return make_response('Internal Server Error', 400)
@app.route('/')
def index():
try:
# Your code that may throw an error.
raise Exception('something went wrong') # raise exception
except Exception as exc:
# Handle exception here!
pass
else:
# This code is executes if no exception occurs.
pass
finally:
# This code is executed despite an exception.
pass
# ...
推荐阅读
- javascript - 如何让我的机器人只回答我编写的命令?
- angular - 使用 AngularFire 的 where 子句在 Angular 中没有按预期工作
- javascript - 创建 TS 数组自定义类型?
- mysql - Wordpress SQL 查询消耗 100% CPU
- angular - 使用 ngFor 指令 Angular 创建多个不同的动态组件
- php - 单个页面中的多个星级评分表
- java - 将 _id(ObjectId) 转换为聚合中的字符串以进行查找 Spring boot
- acumatica - 弹出值未在自定义屏幕网格中更新
- compiler-errors - 调用`std.math.clamp` 给出编译错误`错误:无法在 Zig 中评估常量表达式`
- python - 从具有正确边缘的 png 图像中获取轮廓(轮廓)