本文介绍了如何才能子类endpoints.ServiceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档在的。然而,子类不能真正表达任何字符串消息,state和http代码之外的任何内容。

对于任何具有更智能异常处理的应用程序,错误都需要携带更多



如何才能子类异常类,提供一个自定义的消息/状态?

解决方案

目前,无法扩展有效负载,但您可以自定义状态码。 <$ c对于 400 错误,$ c> endpoints.api_exceptions :

<$ p $ b $ import httplib
class BadRequestException(ServiceException):
请求异常映射到400响应。
http_status = httplib。 BAD_REQUEST

状态码的当前列表(截至2013年5月8日)支持fo r错误是:


  • httplib.BAD_REQUEST :400

  • httplib.UNAUTHORIZED :401

  • httplib.FORBIDDEN :403

  • httplib.NOT_FOUND :404

  • httplib。 CONFLICT

  • httplib.GONE :410

  • httplib.PRECONDITION_FAILED :412

  • httplib.REQUEST_ENTITY_TOO_LARGE :413



这些状态码将被映射到其他代码:


  • httplib.PAYMENT_REQUIRED :402映射到404

  • httplib.METHOD_NOT_ALLOWED :405映射到501

  • httplib.NOT_ACCEPTABLE :406映射到404

  • code> httplib.PROXY_AUTHENTICATION_REQUIRED :407映射到404

  • httplib.REQUEST_TIMEOUT :408映射到503

  • httplib.LENGTH_REQUIRED :411映射到404

  • httplib.REQUEST_URI_TOO_LONG :414映射到404

  • httplib.UNSUPPORTED_MEDIA_TYPE :415映射到404

  • httplib.REQUESTED_RANGE_NOT_SATISFIABLE :416映射到404

  • httplib.EXPECTATION_FAILED :417映射到404



另外,如果您的回复是 message_types.VoidMessage 对象,您将能够发送 204 无内容响应( httplib.NO_CONTENT )。


The documentation mentions "subclassing endpoints.ServiceException" at https://developers.google.com/appengine/docs/python/endpoints/exceptions. However, subclasses can't really express anything beyond a string message, "state" and http code.

For any application to have smarter exception handling, errors need to carry more than this.

How can one subclass the exception class, providing a custom message/state?

解决方案

Currently, there is no way to extend the payload, but you can customize the status code.

As is done in endpoints.api_exceptions for a 400 error:

import httplib
class BadRequestException(ServiceException):
  """Bad request exception that is mapped to a 400 response."""
  http_status = httplib.BAD_REQUEST

The current list (as of 5/8/2013) of status codes supported for errors are:

  • httplib.BAD_REQUEST: 400
  • httplib.UNAUTHORIZED: 401
  • httplib.FORBIDDEN: 403
  • httplib.NOT_FOUND: 404
  • httplib.CONFLICT: 409
  • httplib.GONE: 410
  • httplib.PRECONDITION_FAILED: 412
  • httplib.REQUEST_ENTITY_TOO_LARGE: 413

and these status codes will be mapped to other codes:

  • httplib.PAYMENT_REQUIRED: 402 mapped to 404
  • httplib.METHOD_NOT_ALLOWED: 405 mapped to 501
  • httplib.NOT_ACCEPTABLE: 406 mapped to 404
  • httplib.PROXY_AUTHENTICATION_REQUIRED: 407 mapped to 404
  • httplib.REQUEST_TIMEOUT: 408 mapped to 503
  • httplib.LENGTH_REQUIRED: 411 mapped to 404
  • httplib.REQUEST_URI_TOO_LONG: 414 mapped to 404
  • httplib.UNSUPPORTED_MEDIA_TYPE: 415 mapped to 404
  • httplib.REQUESTED_RANGE_NOT_SATISFIABLE: 416 mapped to 404
  • httplib.EXPECTATION_FAILED: 417 mapped to 404

In addition if your response is a message_types.VoidMessage object, you will be able to send 204 no content response (httplib.NO_CONTENT).

这篇关于如何才能子类endpoints.ServiceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:33