Skip to content

Commit

Permalink
simplify LogErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Jul 23, 2024
1 parent af61c38 commit f544317
Showing 1 changed file with 5 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,7 @@ public class LogErrorHandler implements Handler<RoutingContext> {
@Override
public void handle(final RoutingContext failureContext) {
if (failureContext.failed()) {
final String requestUri =
getRequestUri(failureContext)
.orElse("Vertx failed to calculate request URI due to malformed host header.");

if (failureContext.failure() != null) {
LOG.error(
"Failed request: {} due to {}",
requestUri,
failureContext.failure().getMessage(),
failureContext.failure());
} else {
LOG.error("Failed request: {}", requestUri);
}
LOG.error("Failed request: {}", getRequestUri(failureContext), failureContext.failure());

// Let the next matching route or error handler deal with the error, we only handle logging
failureContext.next();
Expand All @@ -48,12 +36,13 @@ public void handle(final RoutingContext failureContext) {
}
}

private static Optional<String> getRequestUri(final RoutingContext failureContext) {
private static String getRequestUri(final RoutingContext failureContext) {
try {
return Optional.ofNullable(failureContext.request().absoluteURI());
return Optional.ofNullable(failureContext.request().absoluteURI()).orElse("[null uri]");
} catch (final NullPointerException e) {
// absoluteURI() can throw NPE when header host is malformed.
return Optional.empty();
LOG.warn("Vertx failed to calculate request URI due to malformed host header.");
return "[null uri]";
}
}
}

0 comments on commit f544317

Please sign in to comment.