Skip to content

Instantly share code, notes, and snippets.

@yccheok
Created June 22, 2017 19:21
Show Gist options
  • Select an option

  • Save yccheok/60da3889df9b920e1ad3703cbdd0c39d to your computer and use it in GitHub Desktop.

Select an option

Save yccheok/60da3889df9b920e1ad3703cbdd0c39d to your computer and use it in GitHub Desktop.
// Current code
public static ErrorResponse getErrorResponse (Response<?> response) {
final Gson gson = new Gson();
try {
String json = response.errorBody().string();
return gson.fromJson(json, ErrorResponse.class);
} catch (Exception e) {
android.util.Log.e(TAG, "", e);
}
return null;
}
// Not sure this is better, so that caller need not perform null checking.
public static ErrorResponse getErrorResponse (Response<?> response) {
final Gson gson = new Gson();
ErrorResponse errorResponse;
try {
String json = response.errorBody().string();
errorResponse = gson.fromJson(json, ErrorResponse.class);
} catch (Exception e) {
android.util.Log.e(TAG, "", e);
}
if (errorResponse == null) {
return new ErrorResponse(-1, "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment