2017-03-03 1 views
1

병/파이썬 함께 더 자세한 오류 처리를 얻으려고합니다. How to return error messages in JSON with Bottle HTTPError? 방법을 설명하는 페이지가 있지만 내 프로젝트에서이를 구현할 수 없습니다.default_error_handler를 사용하여 병/파이썬 오류 처리

ara.hayrabedian 님의 답변은 언급 한 페이지에서 작동하지만, 오류 상황에 대한 자세한 정보를 얻으려는 의도로 Michael의 코드에는 약간의 매력이 있습니다. 테스트 한 모든 대안 만 실패합니다. 기본적으로 나는 (이상 코딩 중)이 있습니다

답변

-2

: "404 찾을 수 없음 오류"

#!/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가'와, 단지 표준 병 HTML 오류 페이지 호출되지 잘못된 페이지를 호출 그리고 장식품을 @bottle.error() 그냥 사용하지 않는 이유는 무엇입니까? 오류 값을 입력합니다.이 값은 bottle.abort을 통해 전송하는 모든 개체가 될 수 있습니다. 어쨌든 당신이 원하는 기능으로 마무리 할 수 ​​있습니다.