From 91849094cd9b28b5d9a2a6d9c1d3bb4be0cec80e Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 31 May 2024 19:51:24 +0200 Subject: [PATCH] [DEV] Correct all catcher to be named normalized --- .../archidata/catcher/ExceptionCatcher.java | 4 ++-- .../kar/archidata/catcher/GenericCatcher.java | 24 +++++++++++++++++++ ...cher.java => JacksonExceptionCatcher.java} | 8 +++---- ...va => WebApplicationExceptionCatcher.java} | 10 ++++---- 4 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 src/org/kar/archidata/catcher/GenericCatcher.java rename src/org/kar/archidata/catcher/{JacksonCatcher.java => JacksonExceptionCatcher.java} (77%) rename src/org/kar/archidata/catcher/{FailException404API.java => WebApplicationExceptionCatcher.java} (60%) diff --git a/src/org/kar/archidata/catcher/ExceptionCatcher.java b/src/org/kar/archidata/catcher/ExceptionCatcher.java index 6a49218..c732efa 100644 --- a/src/org/kar/archidata/catcher/ExceptionCatcher.java +++ b/src/org/kar/archidata/catcher/ExceptionCatcher.java @@ -21,8 +21,8 @@ public class ExceptionCatcher implements ExceptionMapper { } private RestErrorResponse build(final Exception exception) { - return new RestErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Catch Unknown Exception", - exception.getMessage()); + return new RestErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, + "Catch Unknown Exception: " + exception.getClass().getCanonicalName(), exception.getMessage()); } } diff --git a/src/org/kar/archidata/catcher/GenericCatcher.java b/src/org/kar/archidata/catcher/GenericCatcher.java new file mode 100644 index 0000000..2adc85c --- /dev/null +++ b/src/org/kar/archidata/catcher/GenericCatcher.java @@ -0,0 +1,24 @@ +package org.kar.archidata.catcher; + +import org.glassfish.jersey.server.ResourceConfig; + +public class GenericCatcher { + + /** + * Add All the the generic catcher to standardize returns. + * @param rc Resource exception model. + */ + public static void addAll(final ResourceConfig rc) { + // Generic Json parsing error + rc.register(JacksonExceptionCatcher.class); + // Catch jakarta generic errors + rc.register(WebApplicationExceptionCatcher.class); + // Archidata exceptions + rc.register(InputExceptionCatcher.class); + rc.register(SystemExceptionCatcher.class); + rc.register(FailExceptionCatcher.class); + // generic Exception catcher + rc.register(ExceptionCatcher.class); + } + +} diff --git a/src/org/kar/archidata/catcher/JacksonCatcher.java b/src/org/kar/archidata/catcher/JacksonExceptionCatcher.java similarity index 77% rename from src/org/kar/archidata/catcher/JacksonCatcher.java rename to src/org/kar/archidata/catcher/JacksonExceptionCatcher.java index 19113e0..21a5b40 100644 --- a/src/org/kar/archidata/catcher/JacksonCatcher.java +++ b/src/org/kar/archidata/catcher/JacksonExceptionCatcher.java @@ -3,17 +3,17 @@ package org.kar.archidata.catcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.JacksonException; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; -public class JacksonCatcher implements ExceptionMapper { - private static final Logger LOGGER = LoggerFactory.getLogger(JacksonCatcher.class); +public class JacksonExceptionCatcher implements ExceptionMapper { + private static final Logger LOGGER = LoggerFactory.getLogger(JacksonExceptionCatcher.class); @Override - public Response toResponse(final JsonProcessingException exception) { + public Response toResponse(final JacksonException exception) { LOGGER.warn("Catch exception Input data parsing:"); final RestErrorResponse ret = build(exception); LOGGER.error("Error UUID={}", ret.uuid); diff --git a/src/org/kar/archidata/catcher/FailException404API.java b/src/org/kar/archidata/catcher/WebApplicationExceptionCatcher.java similarity index 60% rename from src/org/kar/archidata/catcher/FailException404API.java rename to src/org/kar/archidata/catcher/WebApplicationExceptionCatcher.java index b0f1c51..1584a5f 100644 --- a/src/org/kar/archidata/catcher/FailException404API.java +++ b/src/org/kar/archidata/catcher/WebApplicationExceptionCatcher.java @@ -3,23 +3,23 @@ package org.kar.archidata.catcher; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import jakarta.ws.rs.ClientErrorException; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.ext.ExceptionMapper; -public class FailException404API implements ExceptionMapper { - private static final Logger LOGGER = LoggerFactory.getLogger(FailException404API.class); +public class WebApplicationExceptionCatcher implements ExceptionMapper { + private static final Logger LOGGER = LoggerFactory.getLogger(WebApplicationExceptionCatcher.class); @Override - public Response toResponse(final ClientErrorException exception) { + public Response toResponse(final WebApplicationException exception) { final RestErrorResponse ret = build(exception); LOGGER.error("Error UUID={}", ret.uuid); return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret) .type(MediaType.APPLICATION_JSON).build(); } - private RestErrorResponse build(final ClientErrorException exception) { + private RestErrorResponse build(final WebApplicationException exception) { return new RestErrorResponse(exception.getResponse().getStatusInfo().toEnum(), "Catch system exception", exception.getMessage()); }