Stay organized with collections
Save and categorize content based on your preferences.
In many situations, you might want to use common HTTP status codes to indicate
the success or failure of a user's API request. For example, if a user is
attempting to retrieve an entity which doesn't exist, you might want to send an
HTTP 404 status code to say that no entity with the ID entity_id exists.
You can send such common HTTP status codes by raising an exception provided
by the endpoints library as follows:
message='No entity with the id "%s" exists.'%entity_idraiseendpoints.NotFoundException(message)
Exceptions provided by Endpoints Frameworks
The endpoints library provides the following exceptions, corresponding to
specific HTTP status codes:
Exception
Corresponding HTTP status code
endpoints.BadRequestException
HTTP 400
endpoints.UnauthorizedException
HTTP 401
endpoints.ForbiddenException
HTTP 403
endpoints.NotFoundException
HTTP 404
endpoints.InternalServerErrorException
HTTP 500
Supported HTTP status codes
Cloud Endpoints Frameworks supports a subset of HTTP status codes
in API responses. The following table describes the supported codes.
HTTP status codes
Support
HTTP 2xx
HTTP 200 is typically assumed by Endpoints Frameworks if the API method returns successfully.If the API method response type is VoidMessage or the return value of the API method is None , HTTP 204 is set instead.
HTTP 3xx
HTTP 3xx codes aren't supported. Use of any HTTP 3xx codes results in an HTTP 404 response.
HTTP 4xx
Only the following HTTP 4xx codes are supported:
400
401
403
404
409
410
412
413
Any other HTTP 4xx codes are returned as error 404, except for the following:
405 is returned as 501
408 is returned as 503
HTTP 5xx
All HTTP 5xx status codes are converted to HTTP 503 in the client response.
Creating your own exception classes
If you want to create other exception classes for other HTTP status codes, you
can do so by subclassing endpoints.ServiceException. The following snippet
shows how to create an exception class that represents an HTTP 409 status code:
importendpointsimporthttplibclassConflictException(endpoints.ServiceException):"""Conflict exception that is mapped to a 409 response."""http_status=httplib.CONFLICT
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-04-09 UTC."],[[["Endpoints Frameworks allows for the use of common HTTP status codes to indicate the success or failure of API requests, such as using a `404` error to indicate an entity does not exist."],["Specific exceptions, like `BadRequestException`, `UnauthorizedException`, `ForbiddenException`, `NotFoundException`, and `InternalServerErrorException`, are provided by the endpoints library and correspond to specific HTTP error codes."],["Only a subset of HTTP status codes are supported, where HTTP `200` or `204` is used for success, 3xx codes result in a `404` error, specific 4xx codes are supported, and all 5xx codes are converted to `503`."],["Uncaught exceptions in the application will result in an HTTP `503` error from the backend API."],["Custom exception classes for other HTTP status codes can be made by subclassing `endpoints.ServiceException`, as shown in the example of creating a `ConflictException` for an HTTP 409 status code."]]],[]]