String detailString = null;
try {
if (!response.isSuccessStatusCode()
&& HttpMediaType.equalsIgnoreParameters(Json.MEDIA_TYPE, response.getContentType())
&& response.getContent() != null) {
JsonParser parser = null;
try {
parser = jsonFactory.createJsonParser(response.getContent());
JsonToken currentToken = parser.getCurrentToken();
// token is null at start, so get next token
if (currentToken == null) {
currentToken = parser.nextToken();
}
// check for empty content
if (currentToken != null) {
// make sure there is an "error" key
parser.skipToKey("error");
if (parser.getCurrentToken() != JsonToken.END_OBJECT) {
details = parser.parseAndClose(GoogleJsonError.class, null);
detailString = details.toPrettyString();
}
}
} catch (IOException exception) {
// it would be bad to throw an exception while throwing an exception
exception.printStackTrace();
} finally {
if (parser == null) {
response.ignore();
} else if (details == null) {
parser.close();
}
}
} else {
detailString = response.parseAsString();
}