title: Retrofit error handling
body: <p>I wrapped my Retrofit code in a class like below. If it's not clear from the code I'm posting it's interacting with a restful service with OAuth.</p>
  
  <p>What would be a good way to do error handling? The REST server returns an error message in json format. I would like to act on that message by throwing some exceptions from my class. I'm trying to do something like below. But is this good design? Is mixing callbacks and exception throwing a good idea? Is there a better way?</p>
  
  <p>With the approach below I could get i18l messages from within my custom exceptions and toast them to the user.</p>
  
  <pre><code>public class RestClient implements IRestClient {
      private IRestAPI api;
  
      /**
       *
       * @param accessToken
       */
      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&lt;Something&gt;() {
              @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 // something else happened...
                  {
                      throw error;
                  }
              }
          });
      }
  
      @Override
      public void deleteSomething(final Callback callback) {
          api.deleteSomething(new Callback&lt;Something&gt;() {
              @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
                  }
                  ...
              }
          });
      }
  
  }
  </code></pre>
  
  <p>Naturally I would have to create my own call back interface with only a success method.</p>