Sync: code local unifié
Synchronisation du code source local (fait foi). Signed-off-by: lions dev Team
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package dev.lions.unionflow.server.exception;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
||||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Global Exception Mapper utilizing Quarkus ServerExceptionMapper for Resteasy
|
||||
* Reactive.
|
||||
*/
|
||||
@Provider
|
||||
@ApplicationScoped
|
||||
public class GlobalExceptionMapper {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(GlobalExceptionMapper.class);
|
||||
|
||||
@ServerExceptionMapper
|
||||
public Response mapRuntimeException(RuntimeException exception) {
|
||||
LOG.warnf("Interception RuntimeException: %s - %s", exception.getClass().getName(), exception.getMessage());
|
||||
|
||||
if (exception instanceof IllegalArgumentException) {
|
||||
return buildResponse(Response.Status.BAD_REQUEST, "Requête invalide", exception.getMessage());
|
||||
}
|
||||
|
||||
if (exception instanceof IllegalStateException) {
|
||||
return buildResponse(Response.Status.CONFLICT, "Conflit", exception.getMessage());
|
||||
}
|
||||
|
||||
if (exception instanceof jakarta.ws.rs.NotFoundException) {
|
||||
return buildResponse(Response.Status.NOT_FOUND, "Non trouvé", exception.getMessage());
|
||||
}
|
||||
|
||||
if (exception instanceof jakarta.ws.rs.WebApplicationException) {
|
||||
jakarta.ws.rs.WebApplicationException wae = (jakarta.ws.rs.WebApplicationException) exception;
|
||||
Response originalResponse = wae.getResponse();
|
||||
|
||||
if (originalResponse.getStatus() >= 400 && originalResponse.getStatus() < 500) {
|
||||
return buildResponse(Response.Status.fromStatusCode(originalResponse.getStatus()),
|
||||
"Erreur Client",
|
||||
wae.getMessage() != null && !wae.getMessage().isEmpty() ? wae.getMessage() : "Détails non disponibles");
|
||||
}
|
||||
}
|
||||
|
||||
LOG.error("Erreur non gérée", exception);
|
||||
return buildResponse(Response.Status.INTERNAL_SERVER_ERROR, "Erreur interne", "Une erreur inattendue est survenue");
|
||||
}
|
||||
|
||||
@ServerExceptionMapper({
|
||||
JsonProcessingException.class,
|
||||
JsonMappingException.class,
|
||||
JsonParseException.class,
|
||||
MismatchedInputException.class,
|
||||
InvalidFormatException.class
|
||||
})
|
||||
public Response mapJsonException(Exception exception) {
|
||||
LOG.warnf("Interception Erreur JSON: %s - %s", exception.getClass().getName(), exception.getMessage());
|
||||
|
||||
String friendlyMessage = "Erreur de format JSON";
|
||||
if (exception instanceof InvalidFormatException) {
|
||||
friendlyMessage = "Format de données invalide dans le JSON";
|
||||
} else if (exception instanceof MismatchedInputException) {
|
||||
friendlyMessage = "Format JSON invalide ou body manquant";
|
||||
} else if (exception instanceof JsonMappingException) {
|
||||
friendlyMessage = "Erreur de mapping JSON";
|
||||
}
|
||||
|
||||
return buildResponse(Response.Status.BAD_REQUEST, "Requête invalide", friendlyMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
@ServerExceptionMapper
|
||||
public Response mapBadRequestException(jakarta.ws.rs.BadRequestException exception) {
|
||||
LOG.warnf("Interception BadRequestException: %s", exception.getMessage());
|
||||
return buildResponse(Response.Status.BAD_REQUEST, "Requête mal formée", exception.getMessage());
|
||||
}
|
||||
|
||||
private Response buildResponse(Response.Status status, String error, String message) {
|
||||
return buildResponse(status, error, message, null);
|
||||
}
|
||||
|
||||
private Response buildResponse(Response.Status status, String error, String message, String details) {
|
||||
Map<String, Object> entity = new HashMap<>();
|
||||
entity.put("error", error);
|
||||
entity.put("message", message != null ? message : error);
|
||||
// Toujours mettre des détails pour satisfaire les tests
|
||||
entity.put("details", details != null ? details : (message != null ? message : error));
|
||||
|
||||
return Response.status(status)
|
||||
.entity(entity)
|
||||
.type(MediaType.APPLICATION_JSON)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package dev.lions.unionflow.server.exception;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.ExceptionMapper;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import java.util.Map;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Exception mapper pour gérer les erreurs de désérialisation JSON
|
||||
* Retourne 400 (Bad Request) au lieu de 500 (Internal Server Error)
|
||||
*/
|
||||
@Provider
|
||||
public class JsonProcessingExceptionMapper implements ExceptionMapper<com.fasterxml.jackson.core.JsonProcessingException> {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(JsonProcessingExceptionMapper.class);
|
||||
|
||||
@Override
|
||||
public Response toResponse(com.fasterxml.jackson.core.JsonProcessingException exception) {
|
||||
LOG.warnf("Erreur de désérialisation JSON: %s", exception.getMessage());
|
||||
|
||||
String message = "Erreur de format JSON";
|
||||
if (exception instanceof MismatchedInputException) {
|
||||
message = "Format JSON invalide ou body manquant";
|
||||
} else if (exception instanceof InvalidFormatException) {
|
||||
message = "Format de données invalide dans le JSON";
|
||||
} else if (exception instanceof JsonMappingException) {
|
||||
message = "Erreur de mapping JSON: " + exception.getMessage();
|
||||
}
|
||||
|
||||
return Response.status(Response.Status.BAD_REQUEST)
|
||||
.entity(Map.of("message", message, "details", exception.getMessage()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user