Change exceptions inheriting from HttpException to accept "Response"

This follows the previous change and puts more information in the
exceptions (like parts of the http request and response) for better
debugging. This also moves the handling of "retry after" to the
exception itself instead of outside.
Also improved the text of one of the exception invocations.
pull/2/head
Tom Hacohen 7 years ago
parent 571c585292
commit ae99a0f53a

@ -39,13 +39,13 @@ abstract class BaseManager {
if (!response.isSuccessful()) {
switch (response.code()) {
case HttpURLConnection.HTTP_UNAVAILABLE:
throw new Exceptions.ServiceUnavailableException("Service unavailable", Long.valueOf(response.header("Retry-After", "0")));
throw new Exceptions.ServiceUnavailableException(response, "Service unavailable");
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new Exceptions.UnauthorizedException("Failed to connect");
throw new Exceptions.UnauthorizedException(response, "Unauthorized auth token");
case HttpURLConnection.HTTP_FORBIDDEN:
ApiError apiError = GsonHelper.gson.fromJson(response.body().charStream(), ApiError.class);
if (apiError.code.equals("service_inactive")) {
throw new Exceptions.UserInactiveException(apiError.detail);
throw new Exceptions.UserInactiveException(response, apiError.detail);
}
default:
// Fall through. We want to always throw when unsuccessful.

@ -13,25 +13,28 @@ import okio.Buffer;
public class Exceptions {
public static class UnauthorizedException extends HttpException {
public UnauthorizedException(String message) {
super(401, message);
public UnauthorizedException(Response response, String message) {
super(response, message);
}
}
public static class UserInactiveException extends HttpException {
public UserInactiveException(String message) {
super(HttpURLConnection.HTTP_FORBIDDEN, message);
public UserInactiveException(Response response, String message) {
super(response, message);
}
}
public static class ServiceUnavailableException extends HttpException {
public long retryAfter;
public ServiceUnavailableException(String message) {
this(message, 0);
}
public ServiceUnavailableException(String message, long retryAfter) {
super(message);
this.retryAfter = retryAfter;
this.retryAfter = 0;
}
public ServiceUnavailableException(Response response, String message) {
super(response, message);
this.retryAfter = Long.valueOf(response.header("Retry-After", "0"));
}
}

@ -43,7 +43,7 @@ public class JournalAuthenticator {
if (response.isSuccessful()) {
return GsonHelper.gson.fromJson(response.body().charStream(), AuthResponse.class).token;
} else if (response.code() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new Exceptions.UnauthorizedException("Username or password incorrect");
throw new Exceptions.UnauthorizedException(response, "Username or password incorrect");
} else {
throw new Exceptions.HttpException(response);
}

Loading…
Cancel
Save