0
我有一個cherrypy應用程序,我在其中實現了一個自定義工具(一個請求過濾器),我已將它附加到before_handler掛鉤上。 貝婁是過濾器實現:從cherrypy返回json before_handler工具
def custom_filter():
method = cherrypy.request.method
if method == 'POST':
print 'check POST token'
try:
request_headers = cherrypy.request.headers
token = request_headers['Authorization']
if not _auth.validate_token(token):
return 'error message'
except:
print 'Error in post filter'
我想是返回一個消息給客戶如果令牌是無效的。 return語句不起作用。是否有可能做到這一點?如果沒有,是否有其他選擇?
你應該提出'cherrypy.HTTPError'左右(也許你自己的錯誤類實例,附有錯誤元數據)。 您可以編寫自定義錯誤處理程序來發送JSON回覆。在你的配置中設置[request.error_response和/或error_page.default](https://github.com/GDG-Ukraine/gdg.org.ua/blob/master/config/dev/app.yml#L138-L139)指向你的功能,[攔截例外](https://github.com/GDG-Ukraine/gdg.org.ua/blob/master/src/GDGUkraine/errors.py#L37-L89) – webKnjaZ
你可以嘗試使用[ before_error_response/after_error_response掛鉤](https://cherrypy.readthedocs.io/en/latest/extend.html#hook-point) – webKnjaZ