文档中提到了https://developers.google.com/appengine/docs/python/endpoints/exceptions中的“endpoints.ServiceException子类化”。然而,除了字符串消息、"state“和http代码之外,子类不能真正表达任何东西。
对于任何具有更智能异常处理的应用程序,错误都需要携带更多信息。
如何子类化exception类,提供自定义消息/状态?
发布于 2013-05-09 05:08:25
目前无法扩展有效负载,但您可以自定义状态码。
正如在endpoints.api_exceptions
中对400
错误所做的那样:
import httplib
class BadRequestException(ServiceException):
"""Bad request exception that is mapped to a 400 response."""
http_status = httplib.BAD_REQUEST
错误支持的当前状态码列表(截至2013年5/8)为:
httplib.BAD_REQUEST
:400httplib.UNAUTHORIZED
:401httplib.FORBIDDEN
:403httplib.NOT_FOUND
:404httplib.CONFLICT
:409httplib.GONE
:410httplib.PRECONDITION_FAILED
:412httplib.REQUEST_ENTITY_TOO_LARGE
:413这些状态代码将映射到其他代码:
httplib.PAYMENT_REQUIRED
:402映射到404httplib.METHOD_NOT_ALLOWED
:405映射到501httplib.NOT_ACCEPTABLE
:406映射到404httplib.PROXY_AUTHENTICATION_REQUIRED
:407映射到404httplib.REQUEST_TIMEOUT
:408映射到503httplib.LENGTH_REQUIRED
:411映射到404httplib.REQUEST_URI_TOO_LONG
:414映射到404httplib.UNSUPPORTED_MEDIA_TYPE
:415映射到404httplib.REQUESTED_RANGE_NOT_SATISFIABLE
:416映射到404httplib.EXPECTATION_FAILED
:417映射到404此外,如果您的响应是message_types.VoidMessage
对象,您将能够向204
发送无内容响应(httplib.NO_CONTENT
)。
https://stackoverflow.com/questions/16399254
复制相似问题