簡體   English   中英

帶有Google端點的自定義HTTP狀態代碼

[英]Custom HTTP Status Codes with Google Endpoints

是否可以使用endpoints.ServiceException的子類返回“不受支持的” HTTP 4xx狀態代碼?

在文檔https://developers.google.com/appengine/docs/python/endpoints/exceptions 第一似乎是說,這是不可能的

僅支持下面列出的HTTP 4xx代碼...使用其他HTTP 4xx代碼將導致HTTP 404響應。

但是過了一會兒才說是嗎?

如果要為其他HTTP狀態代碼創建其他異常類,則可以通過將endpoints.ServiceException子類化來實現。 以下代碼段顯示了如何創建代表HTTP 409狀態代碼的異常類...

如果我將他們的代碼片段應用到我的應用程序中,那么我似乎並不成功-這是不可能還是僅僅是因為我將建議的代碼片段用於自定義異常類使用時出現了錯誤?

我的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

文檔說這些是您唯一可以映射到的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM