Skip to content

Commit

Permalink
Prevent close from hiding causal exceptions #528
Browse files Browse the repository at this point in the history
* Exceptions thrown by a tool can be hidden by unsafe close methods throwing additional exceptions.
  Avoid this by making use of try-with-resources suppressed exception handling in order to surface the
  primary exception as well as the secondary ones.

* Fixes #528
  • Loading branch information
lbergelson committed Apr 6, 2022
1 parent 527e5d7 commit 20b9dd0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,19 @@ protected void onShutdown() {}
/**
* Template method that runs the startup hook, doWork and then the shutdown hook.
*/
@SuppressWarnings("try")
public final Object runTool(){
try {
try(
final AutoCloseableNoCheckedExceptions thisTool =
() -> {
logger.info("Shutting down engine");
onShutdown();
}
) {
logger.info("Initializing engine");
onStartup();
logger.info("Done initializing engine");
return doWork();
} finally {
logger.info("Shutting down engine");
onShutdown();
}
}

Expand Down Expand Up @@ -483,4 +487,8 @@ public final CommandLineParser getCommandLineParser() {
}
return commandLineParser;
}

protected interface AutoCloseableNoCheckedExceptions extends AutoCloseable{
@Override void close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1078,17 +1078,17 @@ public void onTraversalStart() {}
public Object onTraversalSuccess() { return null; }

@Override
@SuppressWarnings("try")
protected final Object doWork() {
try {
//todo update when we move to java 9+
try(final AutoCloseableNoCheckedExceptions thisTool = this::closeTool){
onTraversalStart();
progressMeter.start();
traverse();
if (!progressMeter.stopped()) {
progressMeter.stop();
}
return onTraversalSuccess();
} finally {
closeTool();
}
}

Expand Down

0 comments on commit 20b9dd0

Please sign in to comment.