Skip to content

Instantly share code, notes, and snippets.

@bulwinkel
Last active July 4, 2017 07:51
Show Gist options
  • Save bulwinkel/50f78cb3783f4b4485b1605c562b22e8 to your computer and use it in GitHub Desktop.
Save bulwinkel/50f78cb3783f4b4485b1605c562b22e8 to your computer and use it in GitHub Desktop.
Utility class for checking if a Throwableis a HttpException of a specific codes.
package com.nm.support.okhttp;
import retrofit2.HttpException;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
public final class HttpExceptions {
private HttpExceptions() {
// no instances
}
public static boolean isCode(Throwable throwable, int code) {
return throwable instanceof HttpException && ((HttpException) throwable).code() == code;
}
public static boolean isNotFound(Throwable throwable) {
return isCode(throwable, HTTP_NOT_FOUND);
}
public static boolean isUnauthorized(Throwable throwable) {
return isCode(throwable, HTTP_UNAUTHORIZED);
}
public static boolean isInternalError(Throwable throwable) {
if (throwable instanceof HttpException) {
final int code = ((HttpException) throwable).code();
if (code >= HTTP_INTERNAL_ERROR) return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment