【问题标题】:Custom HTTP Status Codes with Google Endpoints使用 Google Endpoints 自定义 HTTP 状态代码
【发布时间】:2014-08-01 17:59:45
【问题描述】:

是否可以使用 endpoints.ServiceException 的子类返回“不受支持”的 HTTP 4xx 状态代码?

https://developers.google.com/appengine/docs/python/endpoints/exceptionsfirst 的文档似乎说这是不可能的

仅支持下面列出的 HTTP 4xx 代码...使用其他 HTTP 4xx 代码将导致 HTTP 404 响应。

然后只是稍后说它是?

如果您想为其他 HTTP 状态代码创建其他异常类,您可以通过继承 endpoints.ServiceException 来实现。下面的 sn -p 展示了如何创建一个表示 HTTP 409 状态码的异常类...

如果我将他们的 sn-p 应用到我的应用程序中,我似乎没有成功 - 这是不可能的,或者只是我在使用建议的 sn-p 进行自定义异常时出错了类用途?

我的 Python 文件:

# Standard library imports import httplib

# 3rd party imports import endpoints from protorpc import messages from protorpc import message_types from protorpc import remote


package = 'Unprocessable'


class UnprocessableEntityException(endpoints.ServiceException):
    """Unprocessable Entity exception that is mapped to a 422 response."""
    http_status = httplib.UNPROCESSABLE_ENTITY


class ACustomMessage(messages.Message):
    """Greeting that stores a message."""
    important_field = messages.StringField(1)


def process_important_field(important_field):
    a = important_field * 1

@endpoints.api(name='main', version='v1') class UnprocesableTestHandler(remote.Service):
    """ This class handles the creation of entities from a user for storage in
    the data store.
    """

    @endpoints.method(
        ACustomMessage, ACustomMessage, path='test', 
        http_method='POST', name='test.notprocessable'
    )
    def test_improcessable(self, request):
        important_field=request.important_field

        try:
            process_important_field(important_field)
        except TypeError:
            raise UnprocessableEntityException()

        return ACustomMessage(important_field=important_field)

APPLICATION = endpoints.api_server([UnprocesableTestHandler])

以及相关的 YAML:

application: unprocessable
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
# Endpoints handler
- url: /_ah/spi/.*
  script: main.APPLICATION

libraries:
- name: endpoints
  version: 1.0

通过 POST 请求中发送的有效输入,上述情况非常好,但是如果我更改 POST 数据以包含字段“i_field”而不是“important_field”,那么我会得到 503 而不是预期的 422 和以下内容控制台。

ERROR    2014-06-11 15:29:08,686 service.py:191] Encountered unexpected error from ProtoRPC method implementation: KeyError (422)
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
    response = method(instance, request)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1329, in invoke_remote
    return remote_method(service_instance, request)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
    response = method(service_instance, request)
  File "/Users/saffy/Desktop/422Example/main.py", line 43, in test_improcessable
    raise UnprocessableEntityException()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/endpoints-1.0/endpoints/api_exceptions.py", line 31, in __init__
    httplib.responses[self.http_status])
KeyError: 422

【问题讨论】:

    标签: python google-app-engine python-2.7 google-cloud-endpoints


    【解决方案1】:

    文档说这些是您可以映射到的唯一 404 代码:

    400
    401
    403
    404
    405
    408
    409
    410
    412
    413

    所以 422 超出了这个范围。

    默认的是:

    endpoints.BadRequestException HTTP 400
    endpoints.UnauthorizedException HTTP 401
    endpoints.ForbiddenException HTTP 403
    endpoints.NotFoundException HTTP 404

    尝试将您的异常映射到 405-413

    【讨论】:

    • OK - 所以我对文档的新理解是创建其他异常类的文档是正确的,但只能用于 400-405、408-410 和 412 范围内的 4xx 代码- 413.更清楚了,谢谢。
    猜你喜欢
    • 2015-09-10
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多