Created
February 6, 2015 19:19
-
-
Save tolmachevroman/8e4070c3a88fa948898d to your computer and use it in GitHub Desktop.
Custom Error Handler for Retrofit
This file contains hidden or 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
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); } | |
} |
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment