I am using the Drop-wizard jersey client which internally uses the apache http client and in my scenario if there is any error at server side ie it returns status code in 5xx Server error.
Using below code i am checking whether server returns the 5XX code.
if (response.getStatus() / 100 == 5) {
// do some work
}
Similar code is there in the javax.ws.rs.core.Response
class as well, copied below code from there :-
public static Family familyOf(final int statusCode) {
switch (statusCode / 100) {
case 1:
return Family.INFORMATIONAL;
case 2:
return Family.SUCCESSFUL;
case 3:
return Family.REDIRECTION;
case 4:
return Family.CLIENT_ERROR;
case 5:
return Family.SERVER_ERROR;
default:
return Family.OTHER;
}
}
}
Now my code is failing in the checkstyle and PMD rules. saying 100 and 5 is a magic number.
I don't want to compare the return status code which each of the server side exception code in if condition, is there is any other better way available by which i can check if return code is in the family of server error.
Found the solution :-
Using below code we can check whether return code belongs to the SERVER_ERROR
code.
response.getStatusInfo().getFamily() == Response.Status.Family.SERVER_ERROR