[DEV] Correct all catcher to be named normalized

This commit is contained in:
Edouard DUPIN 2024-05-31 19:51:24 +02:00
parent 7b72b08fc0
commit 91849094cd
4 changed files with 35 additions and 11 deletions

View File

@ -21,8 +21,8 @@ public class ExceptionCatcher implements ExceptionMapper<Exception> {
} }
private RestErrorResponse build(final Exception exception) { private RestErrorResponse build(final Exception exception) {
return new RestErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Catch Unknown Exception", return new RestErrorResponse(Response.Status.INTERNAL_SERVER_ERROR,
exception.getMessage()); "Catch Unknown Exception: " + exception.getClass().getCanonicalName(), exception.getMessage());
} }
} }

View File

@ -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);
}
}

View File

@ -3,17 +3,17 @@ package org.kar.archidata.catcher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.ExceptionMapper;
public class JacksonCatcher implements ExceptionMapper<JsonProcessingException> { public class JacksonExceptionCatcher implements ExceptionMapper<JacksonException> {
private static final Logger LOGGER = LoggerFactory.getLogger(JacksonCatcher.class); private static final Logger LOGGER = LoggerFactory.getLogger(JacksonExceptionCatcher.class);
@Override @Override
public Response toResponse(final JsonProcessingException exception) { public Response toResponse(final JacksonException exception) {
LOGGER.warn("Catch exception Input data parsing:"); LOGGER.warn("Catch exception Input data parsing:");
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error UUID={}", ret.uuid);

View File

@ -3,23 +3,23 @@ package org.kar.archidata.catcher;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.MediaType;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper; import jakarta.ws.rs.ext.ExceptionMapper;
public class FailException404API implements ExceptionMapper<ClientErrorException> { public class WebApplicationExceptionCatcher implements ExceptionMapper<WebApplicationException> {
private static final Logger LOGGER = LoggerFactory.getLogger(FailException404API.class); private static final Logger LOGGER = LoggerFactory.getLogger(WebApplicationExceptionCatcher.class);
@Override @Override
public Response toResponse(final ClientErrorException exception) { public Response toResponse(final WebApplicationException exception) {
final RestErrorResponse ret = build(exception); final RestErrorResponse ret = build(exception);
LOGGER.error("Error UUID={}", ret.uuid); LOGGER.error("Error UUID={}", ret.uuid);
return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret) return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret)
.type(MediaType.APPLICATION_JSON).build(); .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", return new RestErrorResponse(exception.getResponse().getStatusInfo().toEnum(), "Catch system exception",
exception.getMessage()); exception.getMessage());
} }