Skip to content

Commit

Permalink
'#1487 Improve exception treatment to save the error in geojson
Browse files Browse the repository at this point in the history
metadata, so it can be better processed in analysis gui.
  • Loading branch information
patrickdalla committed Sep 21, 2023
1 parent 6dcec30 commit ebdcd51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions iped-geo/src/main/java/iped/geo/nominatim/NominatimTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class NominatimTask extends AbstractTask {
public static final String NOMINATIM_ADDRESSTYPE_METADATA = "nominatim:addrestype";
public static final String NOMINATIM_SUBURB_METADATA = "nominatim:suburb";

public static final String JSON_ERROR_PREFIX = "{\"error\"";

static int MAXTOTAL = 200;

/* The http connection pool is static to be used application wide */
Expand All @@ -68,6 +70,8 @@ public class NominatimTask extends AbstractTask {

private NominatimConfig nominatimConfig;

private Exception unresolvedException;

@Override
public List<Configurable<?>> getConfigurables() {
return Arrays.asList(new NominatimConfig());
Expand Down Expand Up @@ -114,6 +118,7 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
unresolvedServer = true;
}
} catch (Exception e) {
unresolvedException = e;
unresolvedServer = true;
}

Expand Down Expand Up @@ -151,14 +156,10 @@ private Future<String> executeQuery(String lat, String longit) {
return content.toString();
}
}
} catch (ClientProtocolException cpe) {
cpe.printStackTrace();
LOGGER.warn(cpe.getMessage());
} catch (Exception e) {
LOGGER.warn(e.getMessage());
e.printStackTrace();
return getErrorJson(e);
}
return null;
});

return f;
Expand Down Expand Up @@ -199,9 +200,16 @@ protected void process(IItem evidence) throws Exception {
}
}

} else {
// add the error message to nominatim metadata
evidence.getMetadata().add(NOMINATIM_METADATA, getErrorJson(unresolvedException));
}
}

private String getErrorJson(Exception unresolvedException2) {
return JSON_ERROR_PREFIX + " : \"" + unresolvedException.getMessage() + "\"}";
}

private void processNominatimResult(IItem evidence, String content) {
evidence.getMetadata().add(NOMINATIM_METADATA, content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public boolean hasUnableToGeoCode() {
TermsEnum te = ssdv.termsEnum();
BytesRef br = te.next();
if (br != null) {
if (br.utf8ToString().startsWith("{\"error\"")) {
if (br.utf8ToString().startsWith(NominatimTask.JSON_ERROR_PREFIX)) {
hasUnableToGeoCode = true;
} else {
hasUnableToGeoCode = false;
Expand Down

0 comments on commit ebdcd51

Please sign in to comment.