Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log exceptions thrown by HttpHandlers in repository integration tests #48991

Merged
merged 4 commits into from
Nov 12, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -67,13 +68,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 +183,21 @@ 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) {
String request = exchange.getRemoteAddress().toString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of weird? :) Why not just do

logger.error(new ParametrizedMessage("Exception when handling request {} {} {}", exchange.getRemoteAddress(),  exchange.getRequestMethod(), exchange.getRequestURI()), e);

? :)

+ " " + exchange.getRequestMethod()
+ " " + exchange.getRequestURI();
logger.error("Exception when handling request {}", request, e);
throw e;
}
};
}
}