【发布时间】: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