diff --git a/pom.xml b/pom.xml index 0ee44828..d06111bc 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,11 @@ + + org.slf4j + slf4j-api + 1.7.21 + com.google.code.findbugs jsr305 diff --git a/src/main/java/org/zalando/problem/spring/web/advice/AdviceTrait.java b/src/main/java/org/zalando/problem/spring/web/advice/AdviceTrait.java index aa49e55d..a11776b0 100644 --- a/src/main/java/org/zalando/problem/spring/web/advice/AdviceTrait.java +++ b/src/main/java/org/zalando/problem/spring/web/advice/AdviceTrait.java @@ -2,6 +2,8 @@ import com.google.gag.annotation.remark.Hack; import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -22,6 +24,7 @@ import org.zalando.problem.spring.web.advice.routing.RoutingAdviceTrait; import org.zalando.problem.spring.web.advice.validation.ValidationAdviceTrait; +import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.StatusType; import java.util.List; import java.util.Optional; @@ -29,7 +32,6 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static java.util.Arrays.asList; import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION; -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST; @@ -66,6 +68,8 @@ */ public interface AdviceTrait { + Logger LOG = LoggerFactory.getLogger(AdviceTrait.class); + default ResponseEntity create(final StatusType status, final Throwable throwable, final NativeWebRequest request) { return create(status, throwable, request, new HttpHeaders()); @@ -126,7 +130,9 @@ default ResponseEntity create(final Throwable throwable, final Problem final NativeWebRequest request, final HttpHeaders headers) { final HttpStatus status = HttpStatus.valueOf(firstNonNull( - problem.getStatus(), INTERNAL_SERVER_ERROR).getStatusCode()); + problem.getStatus(), Status.INTERNAL_SERVER_ERROR).getStatusCode()); + + log(throwable, problem, request, status); if (status == HttpStatus.INTERNAL_SERVER_ERROR) { request.setAttribute(ERROR_EXCEPTION, throwable, SCOPE_REQUEST); @@ -140,6 +146,16 @@ default ResponseEntity create(final Throwable throwable, final Problem .orElseGet(() -> fallback(throwable, problem, request, headers))); } + default void log( + @SuppressWarnings("UnusedParameters") final Throwable throwable, + @SuppressWarnings("UnusedParameters") final Problem problem, + @SuppressWarnings("UnusedParameters") final NativeWebRequest request, + final HttpStatus status) { + if (status == HttpStatus.INTERNAL_SERVER_ERROR) { + LOG.error("Internal Server Error", throwable); + } + } + default ResponseEntity fallback(final Throwable throwable, final Problem problem, final NativeWebRequest request, final HttpHeaders headers) { return ResponseEntity.status(NOT_ACCEPTABLE).body(null);