|
22 | 22 | import com.sun.net.httpserver.HttpHandler;
|
23 | 23 | import com.sun.net.httpserver.HttpServer;
|
24 | 24 | import org.apache.http.HttpStatus;
|
| 25 | +import org.apache.logging.log4j.Logger; |
| 26 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
25 | 27 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
|
26 | 28 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
27 | 29 | import org.elasticsearch.common.Strings;
|
@@ -67,13 +69,7 @@ public static void startHttpServer() throws Exception {
|
67 | 69 | @Before
|
68 | 70 | public void setUpHttpServer() {
|
69 | 71 | 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))); |
77 | 73 | }
|
78 | 74 |
|
79 | 75 | @AfterClass
|
@@ -188,4 +184,19 @@ protected boolean canFailRequest(final HttpExchange exchange) {
|
188 | 184 | return true;
|
189 | 185 | }
|
190 | 186 | }
|
| 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 | + } |
191 | 202 | }
|
0 commit comments