Skip to content

Instantly share code, notes, and snippets.

@niftynei
Last active December 17, 2015 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftynei/0dca2b1a0151788f4ecc to your computer and use it in GitHub Desktop.
Save niftynei/0dca2b1a0151788f4ecc to your computer and use it in GitHub Desktop.
sample catch block
public <ReturnType> NetworkRequest<ReturnType> makeRequest(NetworkRequest<ReturnType> request) {
try {
request.makeRequest();
// if you reach this, request succeeded!
request.setStatus(HttpURLConnection.HTTP_OK, NetworkRequest.Status.SUCCESS);
return request;
} catch (RetrofitError error) {
Response errorResponse = error.getResponse();
switch (error.getKind()) {
case NETWORK:
// handle network errors
break;
case HTTP:
// unexpected response from the server. may be a client error, auth error, server error...
break;
case CONVERSION:
// problem with GSON or marshalling response into a POJO
break;
case UNEXPECTED:
// who knows.
throw error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment