Reputation: 3160
How do I correctly use the resterrorhandler? I use AndroidAnnotations 3.0.1 whitch uses the springresttemplate for android.
I use this example to start with and it works as expected but how am I supposed to use it? I mean if I catch the exception with a custom errorhandler than how do I notify the activity/fragment? With the custom errorhandler my approach would be:
@Background
public void getAuth() {
User u = restClient.getAuth(auth);
//the important part is here
//how do i know here that an error happend except than check user for null?
//more important how do I pass it the Httperrorcode and a custom headerfield?
if (u != null) {
Log.d("XXXX", "Authtoken: " + u.getAuthToken());
}
doLater();
eventManager.fire(Events.Load.dismiss);
}
With the default errorhandler my approach would be:
@Background
public void getAuth() {
try {
User u = restClient.getAuth(auth); //important part here happens a failure
Log.d("XXXX", "Authtoken: " + u.getAuthToken());
doLater();
} catch (RestClientException e) {
Log.e("XXXX", "error..................");
}finally{
eventManager.fire(Events.Load.dismiss);
}
}
Isn't it bad to handle Httperrorcodes in the activity/fragment? If so where should I call it?
Of course an Unchecked Exception can be thrown from the customerrorhandler and pass the parameters within the exception but I still have no idea how to access the headerfields of a response.
Upvotes: 1
Views: 799
Reputation: 333
I'm not sure about if this is how it exactly works (or should work) but in my case, If inject the error handler no exceptions is thrown by the method you called. But I if you doesn't inject the error handler, the call to the rest service throws an exception.
This is what I mean:
try{
validate = restClient.validateUser(email, code);
} catch (Exception e) {
e.printStackTrace();
}
@AfterInject
void afterInject() {
// if the line below is commented, myErrorHandler wouldn't be called to handle errors, and the restClient.method() would throw an exception.
//restClient.setRestErrorHandler(myErrorHandler);
}
Upvotes: 1
Reputation: 78
May not be the most elegant solution, please weigh in if anyone has a better approach.
Of course an Unchecked Exception can be thrown from the customerrorhandler and pass the parameters within the exception but I still have no idea how to access the headerfields of a response.
RestTemplate throws a NestedRuntimeException, specifically a HttpClientErrorException which is wrapped up in a NestedRuntimeException, when a client error occurs. You can do a checked cast back to HttpClientErrorException that lets you access most of (if not all) the information you require.
I use this example to start with and it works as expected but how am I supposed to use it? I mean if I catch the exception with a custom errorhandler than how do I notify the activity/fragment?
In the custom error handler I throw a new custom exception (depending on what situation I'm trying to handle) that contain a friendly message that the activity catches and directly displays. For example, if the server sends a unauthorized 401 response, I throw a new custom exception with the message "You have logged out, Please log back in to continue" which the Activity displays/handles accordingly.
if (runtimeException instanceof HttpClientErrorException) {
HttpClientErrorException exception = (HttpClientErrorException) runtimeException;
if (exception.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
throw new DisplayMessageExcpetion("You have logged out, Please log back in to continue");
} else if (exception.getStatusCode().equals(HttpStatus.BAD_GATEWAY)) {
etc...
//more important how do I pass it the Httperrorcode and a custom headerfield?
Above works if you only need access to the http status and the body of the response, if you need to access custom header fields you will need to provide your own implementation of
void handleError(ClientHttpResponse response) throws IOException;
from ResponseErrorHandler and set it as the error handler on the RestTemplate. Source of DefaultResponseErrorHandler should give you a good idea of how to do it. Refer this for getting/setting RestTemplate from your service interface.
Upvotes: 1