我有以下Bottle应用程序:
from bottle import Bottle, run, request, response, HTTPResponse
APP1 = Bottle()
@APP1.hook('before_request')
def before_request():
print "APP 1 - Before Request {}".format(request.url)
@APP1.hook('after_request')
def after_request():
print "APP 1 - After Request {}".format(request.url)
print "Response status {}".format(response.status_code)
@APP1.route('/hello')
def hello():
return "Hello World!"
@APP1.route('/error')
def error():
raise HTTPResponse(status=400)
if __name__ == "__main__":
run(APP1, host='127.0.0.1', port=8080)
当我点击以下网址时:http://127.0.0.1:8080/error
我希望打印出400的响应代码,而不是打印200.
APP 1 - Before Request http://localhost:8080/error
APP 1 - After Request http://localhost:8080/error
Response status 200
我知道我可以使用插件概念来实现around建议,但这意味着我们不能使用after_request
事件挂钩来处理错误状态代码。此外,它似乎反直觉。
我的设置不正确吗?或者我对瓶子after_request
事件挂钩的理解不正确吗?
请建议。