Skip to content

Instantly share code, notes, and snippets.

@timigod
Created August 13, 2017 14:35
Show Gist options
  • Save timigod/16f0abb0805d9f73e3af108b259745ce to your computer and use it in GitHub Desktop.
Save timigod/16f0abb0805d9f73e3af108b259745ce to your computer and use it in GitHub Desktop.
Class to get JSON body parameters from Retrofit 2 errors
public class ApiError {
public String error = "An error occurred";
public ApiError(Throwable error) {
if (error instanceof HttpException) {
String errorJsonString = null;
try {
errorJsonString = ((HttpException)
error).response().errorBody().string();
} catch (IOException e) {
e.printStackTrace();
}
JsonElement parsedString = new
JsonParser().parse(errorJsonString);
this.error = parsedString.getAsJsonObject()
.get("error")
.getAsString();
} else {
this.error = error.getMessage() != null ? error.getMessage() : this.error;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment