Refactor into separate method
@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -131,11 +132,7 @@ class HttpRequestDispatch {
error,
() -> "Network connection was unexpectedly terminated: " + parent.jettyRequest.getRequestURI());
parent.metricReporter.prematurelyClosed();
- } else if (!(error instanceof CompletionException && error.getCause() instanceof OverloadException
- || error instanceof OverloadException
- || error instanceof BindingNotFoundException
- || error instanceof CompletionException && error.getCause() instanceof BindingNotFoundException
- || error instanceof RequestException)) {
+ } else if (!isErrorOfType(error, OverloadException.class, BindingNotFoundException.class, RequestException.class)) {
log.log(Level.WARNING, "Request failed: " + parent.jettyRequest.getRequestURI(), error);
}
reportedError = true;
@@ -154,6 +151,15 @@ class HttpRequestDispatch {
};
}
+ @SafeVarargs
+ @SuppressWarnings("varargs")
+ private static boolean isErrorOfType(Throwable throwable, Class<? extends Throwable>... handledTypes) {
+ return Arrays.stream(handledTypes)
+ .anyMatch(
+ exceptionType -> exceptionType.isInstance(throwable)
+ || throwable instanceof CompletionException && exceptionType.isInstance(throwable.getCause()));
+ }
+
@SuppressWarnings("try")
private ServletRequestReader handleRequest() throws IOException {
HttpRequest jdiscRequest = HttpRequestFactory.newJDiscRequest(jDiscContext.container, jettyRequest);
