Skip to content

Commit

Permalink
Log exceptions thrown by HttpHandlers in repository integration tests (
Browse files Browse the repository at this point in the history
…#48991)

This commit changes the ESMockAPIBasedRepositoryIntegTestCase so 
that HttpHandler are now wrapped in order to log any exceptions that 
could be thrown when executing the server side logic in repository 
integration tests.
  • Loading branch information
tlrx committed Nov 12, 2019
1 parent 1b2b314 commit 3067147
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -67,13 +69,7 @@ public static void startHttpServer() throws Exception {
@Before
public void setUpHttpServer() {
handlers = createHttpHandlers();
handlers.forEach((c, h) -> {
HttpHandler handler = h;
if (randomBoolean()) {
handler = createErroneousHttpHandler(handler);
}
httpServer.createContext(c, handler);
});
handlers.forEach((c, h) -> httpServer.createContext(c, wrap(randomBoolean() ? createErroneousHttpHandler(h) : h, logger)));
}

@AfterClass
Expand Down Expand Up @@ -188,4 +184,19 @@ protected boolean canFailRequest(final HttpExchange exchange) {
return true;
}
}

/**
* Wrap a {@link HttpHandler} to log any thrown exception using the given {@link Logger}.
*/
private static HttpHandler wrap(final HttpHandler handler, final Logger logger) {
return exchange -> {
try {
handler.handle(exchange);
} catch (final Exception e) {
logger.error(() -> new ParameterizedMessage("Exception when handling request {} {} {}",
exchange.getRemoteAddress(), exchange.getRequestMethod(), exchange.getRequestURI()), e);
throw e;
}
};
}
}

0 comments on commit 3067147

Please sign in to comment.