【发布时间】:2017-07-23 10:18:34
【问题描述】:
对于bottle/python,我试图获得更详细的错误处理。有一个描述方法的页面 How to return error messages in JSON with Bottle HTTPError?,但无法在我的项目中实现。
ara.hayrabedian 在上述页面上的回答有效,但希望获得有关错误情况的更多详细信息,Michael 的代码具有一些魅力。只有我测试的任何变体都失败了。基本上我有(出于更长的编码):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bottle import Bottle, run, static_file, view, template, \
get, post, request, debug
from bottle import route, response, error
import json
app = Bottle()
#class JSONErrorBottle(bottle.Bottle): ### just an not working alternative!?
class JSONErrorBottle(Bottle):
def default_error_handler(app, res):
bottle.response.content_type = 'application/json'
print("XXXXXXX " + json.dumps(dict(error=res.body, status_code=res.status_code)))
return json.dumps(dict(error=res.body, status_code=res.status_code))
app.install(JSONErrorBottle)
def main():
app.run(host = prefs['server'], port = prefs['port'], reloader=False)
if __name__ == '__main__':
rcode = main()
调用一个没有调用'default_error_handler'的无效页面,只是带有“错误:404 Not Found”的标准瓶子html错误页面
【问题讨论】:
标签: python error-handling bottle