Skip to content

Commit 0de3555

Browse files
authored
Log exceptions thrown by HttpHandlers in repository integration tests (elastic#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.
1 parent 33a7f06 commit 0de3555

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESMockAPIBasedRepositoryIntegTestCase.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.sun.net.httpserver.HttpHandler;
2323
import com.sun.net.httpserver.HttpServer;
2424
import org.apache.http.HttpStatus;
25+
import org.apache.logging.log4j.Logger;
26+
import org.apache.logging.log4j.message.ParameterizedMessage;
2527
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
2628
import org.elasticsearch.cluster.metadata.IndexMetaData;
2729
import org.elasticsearch.common.Strings;
@@ -67,13 +69,7 @@ public static void startHttpServer() throws Exception {
6769
@Before
6870
public void setUpHttpServer() {
6971
handlers = createHttpHandlers();
70-
handlers.forEach((c, h) -> {
71-
HttpHandler handler = h;
72-
if (randomBoolean()) {
73-
handler = createErroneousHttpHandler(handler);
74-
}
75-
httpServer.createContext(c, handler);
76-
});
72+
handlers.forEach((c, h) -> httpServer.createContext(c, wrap(randomBoolean() ? createErroneousHttpHandler(h) : h, logger)));
7773
}
7874

7975
@AfterClass
@@ -188,4 +184,19 @@ protected boolean canFailRequest(final HttpExchange exchange) {
188184
return true;
189185
}
190186
}
187+
188+
/**
189+
* Wrap a {@link HttpHandler} to log any thrown exception using the given {@link Logger}.
190+
*/
191+
private static HttpHandler wrap(final HttpHandler handler, final Logger logger) {
192+
return exchange -> {
193+
try {
194+
handler.handle(exchange);
195+
} catch (final Exception e) {
196+
logger.error(() -> new ParameterizedMessage("Exception when handling request {} {} {}",
197+
exchange.getRemoteAddress(), exchange.getRequestMethod(), exchange.getRequestURI()), e);
198+
throw e;
199+
}
200+
};
201+
}
191202
}

0 commit comments

Comments
 (0)