Log exceptions thrown by HttpHandlers in repository integration tests (#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.
This commit is contained in:
Tanguy Leroux 2019-11-12 14:13:36 -05:00
parent 698ebd3d0a
commit 1903505a3f

View File

@ -22,6 +22,8 @@ import com.sun.net.httpserver.HttpExchange;
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;
@ -67,13 +69,7 @@ public abstract class ESMockAPIBasedRepositoryIntegTestCase extends ESBlobStoreR
@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
@ -188,4 +184,19 @@ public abstract class ESMockAPIBasedRepositoryIntegTestCase extends ESBlobStoreR
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;
}
};
}
}