AbstractHealthCheck: extract error handling to a method since the error handling for TimeoutExceptions and other handled exceptions is quite similar. So by extracting this into a method we can avoid some code duplication. Change-Id: I0974e2c341624049bb8c6f37933789ab2f3957b6
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java index 5a8246b..db0657e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java +++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/AbstractHealthCheck.java
@@ -87,20 +87,28 @@ try { checkStatusSummary = resultFuture.get(timeout, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { - log.warn("Check {} timed out", name, e); - Long elapsed = System.currentTimeMillis() - ts; - checkStatusSummary = new StatusSummary(Result.TIMEOUT, ts, elapsed, Collections.emptyMap()); - failureCounterMetric.increment(); - latencyMetric.record(elapsed, TimeUnit.MILLISECONDS); + checkStatusSummary = + handleError(ts, e, String.format("Check %s timed out", name), Result.TIMEOUT); } catch (InterruptedException | ExecutionException e) { - log.warn("Check {} failed while waiting for its future result", name, e); - Long elapsed = System.currentTimeMillis() - ts; - checkStatusSummary = new StatusSummary(Result.FAILED, ts, elapsed, Collections.emptyMap()); - failureCounterMetric.increment(); - latencyMetric.record(elapsed, TimeUnit.MILLISECONDS); + checkStatusSummary = + handleError( + ts, + e, + String.format("Check %s failed while waiting for its future result", name), + Result.FAILED); } return checkStatusSummary; } + private StatusSummary handleError(long ts, Exception e, String message, Result result) { + log.warn(message, e); + Long elapsed = System.currentTimeMillis() - ts; + StatusSummary checkStatusSummary = + new StatusSummary(result, ts, elapsed, Collections.emptyMap()); + failureCounterMetric.increment(); + latencyMetric.record(elapsed, TimeUnit.MILLISECONDS); + return checkStatusSummary; + } + protected abstract Result doCheck() throws Exception; }