Exceptions and HTTP
In many situations, you may 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 does not exist, you may want to send an
HTTP 404 status code saying No entity exists with ID:
entityId
.
You can send such common HTTP status codes by throwing an exception provided by the endpoints library as follows:
String message = "No entity exists with ID: " + entityId;
throw new NotFoundException(message);
Exceptions Provided by Endpoints
The endpoints library provides the following exceptions, corresponding to specific HTTP status codes:
Exception | Corresponding HTTP Status Code |
---|---|
com.google.api.server.spi.response.BadRequestException
|
HTTP 400 |
com.google.api.server.spi.response.UnauthorizedException
|
HTTP 401 |
com.google.api.server.spi.response.ForbiddenException
|
HTTP 403 |
com.google.api.server.spi.response.NotFoundException
|
HTTP 404 |
com.google.api.server.spi.response.ConflictException
|
HTTP 409 |
com.google.api.server.spi.response.InternalServerErrorException
|
HTTP 500 |
com.google.api.server.spi.response.ServiceUnavailableException
|
HTTP 503 |
Supported HTTP Status Codes
Endpoints 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 if the API method returns successfully. |