Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Created February 6, 2015 19:19
Show Gist options
  • Save tolmachevroman/8e4070c3a88fa948898d to your computer and use it in GitHub Desktop.
Save tolmachevroman/8e4070c3a88fa948898d to your computer and use it in GitHub Desktop.
Custom Error Handler for Retrofit
public class BadRequestException extends Exception {
public BadRequestException() { super(); }
public BadRequestException(String message) { super(message); }
public BadRequestException(String message, Throwable cause) { super(message, cause); }
public BadRequestException(Throwable cause) { super(cause); }
}
class CustomErrorHandler implements ErrorHandler {
@Override public Throwable handleError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null && r.getStatus() == 422) {
String message = "";
try {
message = CharStreams.toString(new InputStreamReader(r.getBody().in(), Charsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
return new BadRequestException(message);
}
if (r != null && r.getStatus() == 403) {
return new NotAuthorizedException("Not authorized");
}
return cause;
}
}
public class NotAuthorizedException extends Exception {
public NotAuthorizedException() { super(); }
public NotAuthorizedException(String message) { super(message); }
public NotAuthorizedException(String message, Throwable cause) { super(message, cause); }
public NotAuthorizedException(Throwable cause) { super(cause); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment