Fail properly if MaxMind fails to respond / returns invalid data

This commit is contained in:
Colin McDonald 2016-07-03 16:28:31 -04:00
parent 77c3e49048
commit 527f465c12
2 changed files with 17 additions and 2 deletions

View File

@ -45,7 +45,7 @@ public final class IpIntel {
MaxMindUtils.getInsights(id, (maxMindResult, error2) -> { MaxMindUtils.getInsights(id, (maxMindResult, error2) -> {
if (error2 != null) { if (error2 != null) {
callback.onResult(null, error2); callback.onResult(null, error2);
} else { } else if (maxMindResult != null) {
IpIntel newIpIntel = new IpIntel(id, maxMindResult); IpIntel newIpIntel = new IpIntel(id, maxMindResult);
ipIntelCollection.insertOne(newIpIntel, (ignored, error3) -> { ipIntelCollection.insertOne(newIpIntel, (ignored, error3) -> {
@ -55,6 +55,9 @@ public final class IpIntel {
callback.onResult(newIpIntel, null); callback.onResult(newIpIntel, null);
} }
}); });
} else {
// MaxMind failed to return result
callback.onResult(null, null);
} }
}); });
} }
@ -90,6 +93,12 @@ public final class IpIntel {
return; return;
} }
// MaxMind failed to return result
if (maxMindResult == null) {
createNewIntelFuture.complete();
return;
}
IpIntel newIpIntel = new IpIntel(ip, maxMindResult); IpIntel newIpIntel = new IpIntel(ip, maxMindResult);
ipIntelCollection.insertOne(newIpIntel, (ignored, error3) -> { ipIntelCollection.insertOne(newIpIntel, (ignored, error3) -> {

View File

@ -26,7 +26,13 @@ public class MaxMindUtils {
httpsClient.get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> { httpsClient.get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> {
response.bodyHandler((body) -> { response.bodyHandler((body) -> {
JsonObject bodyJson = new JsonObject(body.toString()); JsonObject bodyJson = new JsonObject(body.toString());
callback.onResult(new MaxMindResult(bodyJson), null);
try {
MaxMindResult maxMindResult = new MaxMindResult(bodyJson);
callback.onResult(maxMindResult, null);
} catch (Exception ignored) {
callback.onResult(null, null);
}
}); });
response.exceptionHandler((error) -> callback.onResult(null, error)); response.exceptionHandler((error) -> callback.onResult(null, error));