Skip to content

Instantly share code, notes, and snippets.

@jaredsburrows
Created June 14, 2017 19:02
Show Gist options
  • Save jaredsburrows/54194a4a9486d2115a3191a0b9f77807 to your computer and use it in GitHub Desktop.
Save jaredsburrows/54194a4a9486d2115a3191a0b9f77807 to your computer and use it in GitHub Desktop.
Retrofit 2 ErrorUtils to replace RetrofitError from Retrofit 1
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import retrofit2.Response;
import retrofit2.adapter.rxjava.HttpException;
public final class ErrorUtils {
private static final String TAG = "ErrorUtils";
private static final int HTTP_RATE_LIMIT = 429;
private ErrorUtils() {
}
public static int getErrorMessage(Throwable throwable) {
final String responseMessage = throwable != null ? throwable.getMessage() : "'throwable' is null";
// IOExceptions, etc SocketTimeoutException, Conversion error
if (throwable instanceof IOException) {
Log.e(TAG, responseMessage, throwable);
return "No Network!";
}
// Any HttpExceptions
if (throwable instanceof HttpException) {
final HttpException error = ((HttpException) throwable);
final Response<?> response = error.response();
final String url = error.response().raw().request().url().toString();
final int responseCode = response != null ? error.code() : 0;
if (responseCode == HttpsURLConnection.HTTP_INTERNAL_ERROR) {
Log.e(TAG, responseMessage, error);
return "Internal error.";
} else if (responseCode == HttpsURLConnection.HTTP_CLIENT_TIMEOUT) {
Log.e(TAG, responseMessage, error);
return "Client time out.";
} else if (responseCode == HTTP_RATE_LIMIT) {
Log.e(TAG, responseMessage, error);
return "Rate limited.";
} else {
Log.e(TAG, responseMessage, error);
// If all else fails
return "Error has occurred. Please try again.";
}
}
// If all else fails
return "Error has occurred. Please try again.";
}
}
import org.junit.Test;
import java.io.IOException;
import java.net.HttpURLConnection;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.adapter.rxjava.HttpException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@SuppressWarnings({"unchecked", "ThrowableInstanceNeverThrown"})
public final class ErrorUtilsTest {
@Test
public void testGetErrorMessageNullThrowable() {
assertThat(ErrorUtils.getErrorMessage(null)).isEqualTo("Error has occurred. Please try again.");
}
@Test
public void testGetErrorMessageThrowableReturnGenericMessage() {
assertThat(ErrorUtils.getErrorMessage(new Throwable())).isEqualTo("Error has occurred. Please try again.");
}
@Test
public void testGetErrorMessageGenericNetworkError() {
assertThat(ErrorUtils.getErrorMessage(new IOException())).isEqualTo("No Network!");
}
@Test
public void testGetErrorMessageGenericHttp500Error() {
final ResponseBody body = mock(ResponseBody.class);
final Response<String> response = Response.error(HttpURLConnection.HTTP_INTERNAL_ERROR, body);
final HttpException exception = new HttpException(response);
assertThat(ErrorUtils.getErrorMessage(exception)).isEqualTo("Internal error.");
}
@Test
public void testGetErrorMessageGenericHttp408Error() {
final ResponseBody body = mock(ResponseBody.class);
final Response<String> response = Response.error(HttpURLConnection.HTTP_CLIENT_TIMEOUT, body);
final HttpException exception = new HttpException(response);
assertThat(ErrorUtils.getErrorMessage(exception)).isEqualTo("Client time out.");
}
@Test
public void testGetErrorMessageGenericHttp429Error() {
final ResponseBody body = mock(ResponseBody.class);
final Response<String> response = Response.error(429, body);
final HttpException exception = new HttpException(response);
assertThat(ErrorUtils.getErrorMessage(exception)).isEqualTo("Rate limited.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment