Google Cloud Endpoints Custom Exception

I have the following method that throws an exception:

@ApiMethod(name = "login")
public Profile getLogin(User user) throws UnauthorizedException {

    if (user == null){
        throw new UnauthorizedException("missing user");
    }

    ...
}

How to catch an error in the AsyncTask android client?

protected Profile doInBackground(Void... unused) {

    Profile profile = null;
    try {
        profile = service.login().execute();

    } catch (Exception e) {
        Log.d("exception", e.getMessage(), e);
    }

    return profile;
}

The above did not catch a UnauthorizedException.

+4
source share
1 answer

The exception occurs on the App Engine server, and not on the Android client. Then you must find a way to send a message to the client in order to inform him that an exception has occurred.

In cloud endpoints and other REST APIs, this is done using an HTTP result code and an HTTP result message.

Cloud Endpoints HTTP. com.google.api.server.spi.response.UnauthorizedException. HTTP 401, HTTP "".

Cloud Endpoints Android, , , HTTP.

0

All Articles