Skip to content

Commit

Permalink
Adding log for DefaultExceptionHandler when JSON parse error ha… (lin…
Browse files Browse the repository at this point in the history
…e#2131)

Motivation:

When a JSON document cannot be converted into Java object, an `IllegalArgumentException` is thrown. And this is converted to 400 status code. But it would be nice to have a log statement in `DefaultExceptionHandler` to log what is wrong with the request

Closes line#2041
  • Loading branch information
sivaalli authored and unknown committed Oct 15, 2019
1 parent 360d91e commit 71939c8
Showing 1 changed file with 10 additions and 3 deletions.
Expand Up @@ -16,6 +16,8 @@

package com.linecorp.armeria.internal.annotation;

import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -47,6 +49,7 @@ final class DefaultExceptionHandler implements ExceptionHandlerFunction {
@Override
public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req, Throwable cause) {
if (cause instanceof IllegalArgumentException) {
log(log -> log.warn("{} Failed processing a request:", ctx, cause));
return HttpResponse.of(HttpStatus.BAD_REQUEST);
}

Expand All @@ -58,11 +61,15 @@ public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req,
return ((HttpResponseException) cause).httpResponse();
}

log(log -> log.warn("{} Unhandled exception from an annotated service:", ctx, cause));

return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}

private static void log(Consumer<Logger> logConsumer) {
if (Flags.annotatedServiceExceptionVerbosity() == ExceptionVerbosity.UNHANDLED &&
logger.isWarnEnabled()) {
logger.warn("{} Unhandled exception from an annotated service:", ctx, cause);
logConsumer.accept(logger);
}

return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

0 comments on commit 71939c8

Please sign in to comment.