首页 > 解决方案 > 烧瓶如何在使用 @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

标签: pythonflaskerror-handling

解决方案


您如何看待将可能的错误执行集成到 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 
    # ...

推荐阅读