How could one of the subclasses endpoints.ServiceException be?

The documentation mentions "subclassing endpoints.ServiceException" at https://developers.google.com/appengine/docs/python/endpoints/exceptions . However, subclasses cannot really express anything behind a string message, status, and http code.

For any application with smarter exception handling, errors should carry more than that.

How can I subclass an exception class by providing a custom message / state?

0
source share
1 answer

There is currently no way to expand the payload, but you can set up a status code.

How this is done in endpoints.api_exceptions for error 400 :

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

Current list (as of 5/8/2013) of status codes supported for errors:

  • 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 displayed on 404
  • httplib.METHOD_NOT_ALLOWED : 405 compared to 501
  • httplib.NOT_ACCEPTABLE : 406 displayed on 404
  • httplib.PROXY_AUTHENTICATION_REQUIRED : 407 maps to 404
  • httplib.REQUEST_TIMEOUT : 408 maps to 503
  • httplib.LENGTH_REQUIRED : 411 maps to 404
  • httplib.REQUEST_URI_TOO_LONG : 414 displayed on 404
  • httplib.UNSUPPORTED_MEDIA_TYPE : 415, mapped to 404
  • httplib.REQUESTED_RANGE_NOT_SATISFIABLE : 416 displayed on 404
  • httplib.EXPECTATION_FAILED : 417 displayed on 404

Also, if your response is a message_types.VoidMessage object, you can send 204 no response to the content ( httplib.NO_CONTENT ).

+3
source

All Articles