[英]ValueError: View function did not return a response not caught by Exception exception handler
我在 Flask 應用程序中添加了一個異常處理程序方法,以捕獲在請求處理期間發生的所有意外錯誤,以便我可以控制將哪個響應返回給客戶端。
@app.errorhandler(Exception)
def handle_default(error):
utils.log('Unexpected error happened: ' + str(error))
...
但是,在處理請求(不返回響應)各自的異常時,似乎是否存在編程錯誤
ValueError: View function did not return a response
隨后沒有按照我的預期由這個默認處理程序處理。
為什么會這樣? 關於這個還能做什么?
所有 Flask 路由都需要隱式響應
from flask import Flask
app = Flask("the_flask_module")
@app.route("/")
def home_page():
return "I'm An Implicit Response!"
app.run(host="0.0.0.0", port=5000)
或明確的回應
from flask import Flask, make_response
app = Flask("the_flask_module")
@app.route("/")
def home_page():
response = make_response("I'm an explicit Response!")
response.headers['Content-Type'] = "Malarkey"
response.headers['Nonsense'] = "Seattle Never Has Traffic"
return response
app.run(host="0.0.0.0", port=5000)
這包括錯誤處理程序
@app.errorhandler(Exception)
def handle_default(error):
utils.log('Unexpected error happened: ' + str(error))
...
return "A string or response object."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.