From c6654a177b3930ef9ec68c7d2a8eb8571fac4b1b Mon Sep 17 00:00:00 2001 From: Rob Vesse Date: Mon, 17 Jun 2024 10:14:27 +0100 Subject: [PATCH] FallbackExceptionMapper logs stack traces (CORE-288) The FallbackExceptionMapper exists to capture exceptions that aren't otherwise handled by a JAX-RS application and map them into nice JSON responses. However, in some cases where the error message is very succint, or generic, it doesn't help developers pinpoint where that error actually occurred in their code so they can debug and fix it. The mapper is modified to now explicitly log the stack trace for the error at ERROR level in the logs to aid developers in diagnosing the root cause of such errors. --- CHANGELOG.md | 6 ++++++ .../cache/server/jaxrs/errors/FallbackExceptionMapper.java | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb1a306..84e7239 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +# 0.20.1 + +- JAX-RS Base Server Improvements: + - `FallbackExceptionMapper` explicitly logs the stack trace for otherwise unhandled exceptions to aid in diagnosis + of the underlying issue + # 0.20.0 - Build improvements: diff --git a/jaxrs-base-server/src/main/java/io/telicent/smart/cache/server/jaxrs/errors/FallbackExceptionMapper.java b/jaxrs-base-server/src/main/java/io/telicent/smart/cache/server/jaxrs/errors/FallbackExceptionMapper.java index aca8352..3d3f6ba 100644 --- a/jaxrs-base-server/src/main/java/io/telicent/smart/cache/server/jaxrs/errors/FallbackExceptionMapper.java +++ b/jaxrs-base-server/src/main/java/io/telicent/smart/cache/server/jaxrs/errors/FallbackExceptionMapper.java @@ -20,6 +20,8 @@ import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.Provider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Maps otherwise unhandled errors into RFC 7807 Problem responses @@ -27,6 +29,8 @@ @Provider public class FallbackExceptionMapper implements ExceptionMapper { + private static final Logger LOGGER = LoggerFactory.getLogger(FallbackExceptionMapper.class); + private String buildDetail(Throwable e) { StringBuilder builder = new StringBuilder(); while (e != null) { @@ -55,6 +59,9 @@ public Response toResponse(Exception exception) { //@formatter:on } + // Explicitly log the error with its stack trace for diagnostic purposes + LOGGER.error("Unhandled exception, see stack trace for more detail:", exception); + // For any other error just translate into a 500 Internal Server Error //@formatter:off return new Problem("InternalServerError",