在用於異常處理的雲端點documentation中,建議子類endpoints.ServiceException
類爲409個衝突錯誤提供自定義http_status
。這answer到另一個問題表明,一些支持的狀態代碼被谷歌的基礎設施映射到其他狀態代碼,但409不是映射的狀態代碼之一。endpoints.ServiceException子類返回400狀態碼而不是409
從文檔使用ConflictException
類:
import endpoints
import httplib
class ConflictException(endpoints.ServiceException):
"""Conflict exception that is mapped to a 409 response."""
http_status = httplib.CONFLICT
當我提高ConflictException
:
@endpoints.method(request_message=apimodels.ClientMessage,
response_message=apimodels.ClientMessage,
name='insert',
path='/clients',
http_method='POST'
)
def insert(self, request):
client = models.Client.get_by_id(request.client_code)
if client:
raise ConflictException('Entity with the id "%s" exists.' % request.client_code)
...
我得到一個400錯誤請求的響應:
400 Bad Request
Content-Length: 220
Content-Type: application/json
Date: Thu, 27 Feb 2014 16:11:36 GMT
Server: Development/2.0
{
"error": {
"code": 400,
"errors": [
{
"domain": "global",
"message": "Entity with the id \"FOO\" exists.",
"reason": "badRequest"
}
],
"message": "Entity with the id \"FOO\" exists."
}
}
我在本地dev_appserver上獲得了相同的400響應代碼d部署到App Engine(在1.9.0上)。進入App Engine ProtoRPC代碼,以下line似乎將所有remote.ApplicationError
類型映射到400狀態代碼。
如果我更新endpoints.apiserving._ERROR_NAME_MAP
字典添加我的自定義ConflictException
類,我能夠返回409成功:
import endpoints
import httplib
from endpoints.apiserving import _ERROR_NAME_MAP
class ConflictException(endpoints.ServiceException):
"""Conflict exception that is mapped to a 409 response."""
http_status = httplib.CONFLICT
_ERROR_NAME_MAP[httplib.responses[ConflictException.http_status]] = ConflictException
這是落實endpoints.ServiceException
子類的正確方法是什麼?
我剛剛發佈了同樣的問題,儘管我的研究做得很少:http://stackoverflow.com/q/26318221/635125。我應該讓它開放,因爲它很新鮮並且可能會得到答案?更好的是,Brian,你提交了一個錯誤報告嗎? –
我提出了一個錯誤:https://code.google.com/p/googleappengine/issues/detail?id=11371 –