From a72957648ee2a07382d5df10b2f8a15f124a318b Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Wed, 3 Dec 2025 15:47:12 +0100 Subject: [PATCH] Refactor PrestashopClient to improve image upload response handling and enhance error reporting --- .../service/prestashop/PrestashopClient.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java b/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java index d9cc489..0140435 100644 --- a/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java +++ b/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java @@ -296,7 +296,6 @@ public class PrestashopClient { byte[] multipartBytes = baos.toByteArray(); - // -------- Envoi via HttpURLConnection -------- // -------- Envoi via HttpURLConnection -------- URL targetUrl = URI.create(url).toURL(); HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection(); @@ -335,16 +334,33 @@ public class PrestashopClient { log.info("[PrestaShop] Image upload response status={}, body={}", status, responseBody); + // ---------- Mapping vers une réponse propre pour le front ---------- + + if (status >= 200 && status < 300) { + // Succès : on renvoie un petit JSON que Angular sait lire + return ResponseEntity + .ok() + .contentType(MediaType.APPLICATION_JSON) + .body("{\"success\":true}"); + } + HttpStatus springStatus = HttpStatus.resolve(status); if (springStatus == null) { springStatus = HttpStatus.INTERNAL_SERVER_ERROR; } - return new ResponseEntity<>(responseBody, springStatus); + // En cas d’erreur Presta, on propage l’XML pour debug + return ResponseEntity + .status(springStatus) + .contentType(MediaType.APPLICATION_XML) + .body(responseBody); } catch (IOException e) { log.error("[PrestaShop] Erreur lors de l'upload d'image", e); - throw new RuntimeException("Erreur lors de l'upload d'image vers PrestaShop", e); + return ResponseEntity + .status(HttpStatus.BAD_GATEWAY) + .contentType(MediaType.TEXT_PLAIN) + .body("Erreur lors de l'upload d'image vers PrestaShop"); } } } \ No newline at end of file