endpoints.ServiceException子类返回400状态码而不是409 [英] endpoints.ServiceException subclass returning 400 status code instead of 409

查看:142
本文介绍了endpoints.ServiceException子类返回400状态码而不是409的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于异常处理的Cloud Endpoints 文档中,它是建议子类化 endpoints.ServiceException 类,为409冲突错误提供自定义 http_status 。对另一个问题的回答表明,一些受支持的状态代码被Google的基础架构映射到其他状态代码,但是409 isn '

使用文档中的 ConflictException 类:

 导入端点
导入httplib

类ConflictException(endpoints.ServiceException):
冲突异常映射到409响应。
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)
如果客户端:
引发ConflictException('存在id为'%s'的实体''%request.client_code)

。 ..

我收到400个错误的请求作为回应:

  400错误请求

内容长度:220
内容类型:application / json
日期:星期四,2014年2月27日16:11:36 GMT
服务器:开发/2.0

{
错误:{
code:400,
错误:[
{
domain:global,
message:存在ID为\FOO \的实体。,
reason:badRequest
}
],
message:存在ID为FOO的实体。
}
}

本地dev_appserver并部署到App Engine(在1.9.0上)。进入App Engine ProtoRPC代码,以下 line 似乎将所有 remote.ApplicationError 类型映射到400状态码。

如果我更新 endpoints.apiserving._ERROR_NAME_MAP 字典以添加我的自定义 ConflictException class,我可以成功返回409:

  import endpoints 
import httplib
from endpoints.apiserving import _ERROR_NAME_MAP
$ b $ class ConflictException(endpoints.ServiceException):
映射到409响应的冲突异常。
http_status = httplib.CONFLICT


_ERROR_NAME_MAP [httplib.responses [ConflictException.http_status]] = ConflictException

这是实现 endpoi的正确方法nts.ServiceException 子类?

解决方案

Chris提交的错误报告


In the Cloud Endpoints documentation for exception handling, it is recommended to subclass the endpoints.ServiceException class to provide a custom http_status for 409 Conflict errors. This answer to another question indicates that some of the supported status codes get mapped by Google's infrastructure to other status codes, but 409 isn't one of the mapped status codes.

Using the ConflictException class from the documentation:

import endpoints
import httplib

class ConflictException(endpoints.ServiceException):
    """Conflict exception that is mapped to a 409 response."""
    http_status = httplib.CONFLICT

When I raise the 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)

    ...

I'm getting a 400 Bad Request as the response:

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."
 }
}

I'm getting the same 400 response code on both the local dev_appserver and deployed to App Engine (on 1.9.0). Stepping into the App Engine ProtoRPC code, the following line appears to be mapping all remote.ApplicationError types to a 400 status code.

If I update the endpoints.apiserving._ERROR_NAME_MAP dict to add my custom ConflictException class, I'm able to return a 409 successfully:

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

Is this the correct way to implement endpoints.ServiceException subclasses?

解决方案

It seems to be a bug as according to the bug report filed by Chris.

这篇关于endpoints.ServiceException子类返回400状态码而不是409的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆