gpt4 book ai didi

google-app-engine - endpoints.ServiceException 子类返回 400 状态代码而不是 409

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:20 25 4
gpt4 key购买 nike

在云端点中 documentation对于异常处理,建议子类化 endpoints.ServiceException 类,为 409 冲突错误提供自定义 http_status。这answer另一个问题表明一些支持的状态代码被谷歌的基础设施映射到其他状态代码,但 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 Bad Request 作为响应:

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 和部署到 App Engine(在 1.9.0 上)上都得到相同的 400 响应代码。进入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 子类的正确方法吗?

最佳答案

根据 bug report 这似乎是一个错误由克里斯提交。

关于google-app-engine - endpoints.ServiceException 子类返回 400 状态代码而不是 409,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22075103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com