2012-02-29 57 views
0

返回錯誤類型響應中錯誤代碼等額外數據的最佳方式是什麼? #方法1不會攜帶額外的信息,而#方法2不起作用。在錯誤響應中攜帶額外信息

#method 1 
ImmediateHttpResponse(HttpBadRequest('error')) #cant carry extra info 


#method 2 
class ErrorInfo(object): 
    msg_id = '' 
    msg_code = '' 
    msg_content = '' 

class Resource(Resource): 
    .... 
    def get_object_list(self, request): 
     if ...: 
      error_info = ErrorInfo() 
      error_info.msg_id = 'rt' 
      error_info.msg_code = 'rerer' 
      obj = {'objects': error_info} 
      return self.create_response(request, obj) 
+0

界定 '攜帶額外的信息'。 – jpic 2012-02-29 12:11:42

回答

1

如果有幫助,你可以使用ImmediateHttpResponse(HttpResquest([...],狀態= 403))

+0

是的,有效。提高ImmediateHttpResponse(HttpBadRequest([{'msg_id':'1','msg_code':'r4r'}],status = 403)) – user1076881 2012-02-29 13:28:24

+0

status = 403用於「Forbidden」。對於錯誤的請求,請使用status = 400。當你使用HttpBadRequest時,默認是400。 – jpic 2012-02-29 13:35:03