Marcel Overdijk
Marcel Overdijk

Reputation: 11467

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.

Upvotes: 5

Views: 2175

Answers (3)

Sathya
Sathya

Reputation: 1086

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

Upvotes: 3

davibq
davibq

Reputation: 1099

Im doing this by having an abstract class Entity.

It has a ResponseCode, which is an enum and a String ErrorMessage.

So "Book" could inherit from Entity. And your response could look like this:

public Book insertBook(Book book) {
    PersistenceManager mgr = getPersistenceManager();
    try {
        if (containsShout(book)) {
            book.setResponseCode(ResponseCode.ERROR);
            book.setError("Object already exists");
        } else {
            mgr.makePersistent(book);
        }
    } finally {
        mgr.close();
    }
    return book;
}

Upvotes: 0

Gabriel Ittner
Gabriel Ittner

Reputation: 1162

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.

Upvotes: 5

Related Questions