2017-03-03 11 views
1

ボトル/ Pythonの場合より詳細なエラー処理を試みています。メソッド How to return error messages in JSON with Bottle HTTPError?を記述するページがありますが、それを私のプロジェクトで実装することはできません。ボトル/ pythonのエラー処理with default_error_handler

ara.hayrabedianの回答は上記のページで動作しますが、エラーの状況について詳細を知りたいという希望で、Michaelのコードにはいくつかの魅力があります。私がテストしたバリエーションだけが失敗します。

答えて

-2

#!/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が見つかりませんエラー」とだけ標準ボトルのHTMLエラーページを呼び出す:基本的に私は(長いコーディングの外に)持っていますそして、ちょうど@bottle.error()デコレータを使用するのはなぜですか?それはbottle.abortで送信したオブジェクトのどれでもよいエラーの値を入力します。とにかく、あなたは関数にしたいと思っています。