Commit a2f33b0a authored by Thibault Ehrhart's avatar Thibault Ehrhart

Fix crash with Media API if invalid JSON is returned

parent 5f0844e7
......@@ -204,10 +204,17 @@ public final class AgenceFrancePresseMethods {
final HttpClient httpClient = HttpClientBuilder.create().build();
final HttpResponse res = httpClient.execute(req);
JSONObject data = new JSONObject(IOUtils.toString(res.getEntity().getContent(), "UTF-8"));
final String content = IOUtils.toString(res.getEntity().getContent(), "UTF-8");
JSONObject data = null;
try {
data = new JSONObject(IOUtils.toString(res.getEntity().getContent(), "UTF-8"));
} catch (JSONException e) {
AgenceFrancePresseMethods.LOGGER.error(
"Error when parsing response from Media API: " + e.toString());
}
EntityUtils.consume(res.getEntity());
if (data.get("response") != null) {
if (data != null && data.get("response") != null) {
final JSONObject response = (JSONObject) data.get("response");
if (response.get("docs") != null) {
final JSONArray docs = (JSONArray) response.get("docs");
......@@ -224,7 +231,7 @@ public final class AgenceFrancePresseMethods {
}
}
} catch (URISyntaxException | IOException e) {
AgenceFrancePresseMethods.LOGGER.error(e.toString());
AgenceFrancePresseMethods.LOGGER.error("Error when querying Media API: " + e.toString());
}
return images;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment