Last active
July 4, 2017 07:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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