public class RetrofitException extends RuntimeException {
public static RetrofitException httpError(String url, Response response) {
String message = response.code() + " " + response.message();
return new RetrofitException(message, url, response, Kind.HTTP, null);
}
public static RetrofitException networkError(IOException exception) {
String message = MyApp.getAppContext().getResources().getString(R.string.network_error);
exception.printStackTrace();
return new RetrofitException(message, null, null, Kind.NETWORK, exception);
}
public static RetrofitException unexpectedError(Throwable exception) {
String message = MyApp.getAppContext().getResources().getString(R.string.unexpected_error);
exception.printStackTrace();
return new RetrofitException(message, null, null, Kind.UNEXPECTED, exception);
}
private final String url;
private final Response response;
private final Kind kind;
public RetrofitException(String message, String url, Response response, Kind kind,
Throwable exception) {
super(message, exception);
this.url = url;
this.response = response;
this.kind = kind;
}
/** The request URL which produced the error. */
public String getUrl() {
return url;
}
/** Response object containing status code, item_data, body, etc. */
public Response getResponse() {
return response;
}
/** The event kind which triggered this error. */
public Kind getKind() {
return kind;
}
}