Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning errors with Google Cloud Endpoints

I have an endpoint generated as follows:

public Book insertBook(Book book) {
    PersistenceManager mgr = getPersistenceManager();
    try {
        if (containsShout(book)) {
            throw new EntityExistsException("Object already exists");
        }
        mgr.makePersistent(book);
    } finally {
        mgr.close();
    }
    return book;
}

I wonder how I should return errors to the client. E.g. the book contains some required fields, an ISNM check etc.

So I would assume throwing an exception but how does this map to the returned json response. The json repsonse should contain all field errors to highlight these fields in the client.

like image 642
Marcel Overdijk Avatar asked Mar 02 '13 10:03

Marcel Overdijk


People also ask

How do I see Google API errors?

Google APIs define a set of standard error payloads for error details, which you can find in google/rpc/error_details. proto. These cover the most common needs for API errors, such as quota failure and invalid parameters. Like error codes, developers should use these standard payloads whenever possible.

What is error reporting in GCP?

Error Reporting counts, analyzes, and aggregates the crashes in your running cloud services. A centralized error management interface displays the results with sorting and filtering capabilities.

Is cloud endpoints an API gateway?

Cloud Endpoints is a user-managed service whereas API Gateway is a fully managed service. Both support the same OpenAPI definition format. The main difference is that API Gateway can route a request to multiple backends, but Cloud Endpoints can route traffic only to a single backend.

What are endpoints in Google Cloud?

Endpoints is an API management system that helps you secure, monitor, analyze, and set quotas on your APIs using the same infrastructure Google uses for its own APIs.


2 Answers

In general exceptions are mapped to a 500 http status code in the response. With the following exceptions you can get different codes: com.google.api.server.spi.response.BadRequestException -> 400 com.google.api.server.spi.response.UnauthorizedException -> 401 com.google.api.server.spi.response.ForbiddenException -> 403 com.google.api.server.spi.response.NotFoundException -> 404

If you consume your endpoint in Android the error code will be in the IOException thrown there and you can react accordingly in the catch.

like image 168
Gabriel Ittner Avatar answered Oct 01 '22 01:10

Gabriel Ittner


I tried something like this and seemed to work well for me.

class Response<T> {
   Status status; 
   String userFriendlyMessage;
   T object; //your bean or entity object

   RestResponse toRestResponse() {
      RestResponse r = new RestResponse();
      r.status = status;
      r.userFriendlyMessage = userFriendlyMessage;
      r.object = object;
   }
}

You cannot return a generic object from endpoint. So create an equivalent RestResponse class which can be created from Response.

class RestResponse {
   Status status;
   String userFriendlyMessage;
   Object object;
}

Status can be like this.

public enum Status {
   SUCCESS, RESOURCE_NOT_FOUND, RESOURCE_ALREADY_EXISTS; //etc
}

All your endpoint methods would return RestResponse which will in turn be constructed from a Response (T can be your bean or entity object).

When you deserialize your json response (RestResponse) you can straight away deserialize it as Response.

Hope this helps.

Regards, Sathya

like image 33
Sathya Avatar answered Oct 01 '22 03:10

Sathya