I wrapped my updated code in the class as shown below. If this is not clear from the code, I submit it by interacting with the calming service with OAuth.
What would be a good way to handle errors? The REST server returns an error message in json format. I would like to act on this post by throwing some exceptions from my class. I am trying to do something like below. But is this a good design? Does callbacks and exception mix, throwing a good idea? Is there a better way?
With the approach below, I could receive i18l messages from my user exceptions and push them towards the user.
public class RestClient implements IRestClient {
private IRestAPI api;
public RestClient(final String accessToken)
{
RequestInterceptor requestInterceptor = new RequestInterceptor()
{
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", "Bearer " + accessToken);
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Config.ENDPOINT)
.setRequestInterceptor(requestInterceptor)
.build();
api = restAdapter.create(IRestAPI.class);
}
@Override
public void requestSomething(final Callback callback) {
api.getSomething(new Callback<Something>() {
@Override
public void success(Something something, Response response) {
callback.success(something, response);
}
@Override
public void failure(RetrofitError error) {
if(error.getMessage().getId().euqals(ACCESS_TOKEN_EXPIRED))
{
throw new AccessTokenExpired();
}
else if(error.getMessage().getId().euqals(USER_NOT_FOUND))
{
throw new UsernamePasswordNotFound();
}
else
{
throw error;
}
}
});
}
@Override
public void deleteSomething(final Callback callback) {
api.deleteSomething(new Callback<Something>() {
@Override
public void success(Something something, Response response) {
callback.success(something, response);
}
@Override
public void failure(RetrofitError error) {
if(error.getMessage().getId().euqals(SOMETHING_NOT_FOUND))
{
...
...
Different exceptions
}
...
}
});
}
}
Naturally, I would only have to create my own callback interface using the success method.