Created
August 29, 2014 19:45
-
-
Save benvium/66bf24e0de80d609dac0 to your computer and use it in GitHub Desktop.
Fairly simply Retrofit custom error handling example. Is set up so that you don't need to do much work in the 'failure' handler of a retrofit call to get the user-visible error message to show. Works on all endpoints. There's lots of exception handling as our server folks like to keep us on our toes by sending all kinds of random stuff..!
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
// on error the server sends JSON | |
/* | |
{ "error": { "data": { "message":"A thing went wrong" } } } | |
*/ | |
// create model classes.. | |
public class ErrorResponse { | |
Error error; | |
public static class Error { | |
Data data; | |
public static class Data { | |
String message; | |
} | |
} | |
} | |
// | |
/** | |
* Converts the complex error structure into a single string you can get with error.getLocalizedMessage() in Retrofit error handlers. | |
* Also deals with there being no network available | |
* | |
* Uses a few string IDs for user-visible error messages | |
*/ | |
private static class CustomErrorHandler implements ErrorHandler { | |
private final Context ctx; | |
public CustomErrorHandler(Context ctx) { | |
this.ctx = ctx; | |
} | |
@Override | |
public Throwable handleError(RetrofitError cause) { | |
String errorDescription; | |
if (cause.isNetworkError()) { | |
errorDescription = ctx.getString(R.string.error_network); | |
} else { | |
if (cause.getResponse() == null) { | |
errorDescription = ctx.getString(R.string.error_no_response); | |
} else { | |
// Error message handling - return a simple error to Retrofit handlers.. | |
try { | |
ErrorResponse errorResponse = (ErrorResponse) cause.getBodyAs(ErrorResponse.class); | |
errorDescription = errorResponse.error.data.message; | |
} catch (Exception ex) { | |
try { | |
errorDescription = ctx.getString(R.string.error_network_http_error, cause.getResponse().getStatus()); | |
} catch (Exception ex2) { | |
Log.e(TAG, "handleError: " + ex2.getLocalizedMessage()); | |
errorDescription = ctx.getString(R.string.error_unknown); | |
} | |
} | |
} | |
} | |
return new Exception(errorDescription); | |
} | |
} | |
// When creating the Server... | |
retrofit.RestAdapter restAdapter = new retrofit.RestAdapter.Builder() | |
.setEndpoint(apiUrl) | |
.setLogLevel(retrofit.RestAdapter.LogLevel.FULL) | |
.setErrorHandler(new CustomErrorHandler(ctx)) // use error handler.. | |
.build(); | |
server = restAdapter.create(Server.class); | |
// Now when calling server methods, get simple error out like this: | |
server.postSignIn(login, new Callback<HomePageResponse>() { | |
@Override | |
public void success(HomePageResponse homePageResponse, Response response) { | |
// Do success things! | |
} | |
@Override | |
public void failure(RetrofitError error) { | |
error.getLocalizedMessage(); // <-- this is the message to show to user. | |
} | |
}); |
Thanks
awesome
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool 👍