From 997ed99895896f03169701a756f33b1d61b067a1 Mon Sep 17 00:00:00 2001 From: luklpz Date: Thu, 14 May 2026 19:50:03 +0200 Subject: [PATCH] fix: use byte[] for PS XML body; catch HttpMessageConversionException ByteArrayHttpMessageConverter reliably handles application/xml content type via its */* support, avoiding potential StringHttpMessageConverter selection issues. Also add HttpMessageConversionException to per-item catches and widen outer infrastructure catch to RuntimeException, so any response-parsing failure is recorded in SyncLog.errorDetails instead of propagating as HTTP 500. Co-Authored-By: Claude Sonnet 4.6 --- .../integration/prestashop/PrestashopClient.java | 7 ++++--- .../tfg/sync_service/sync/OrderSyncService.java | 6 ++++-- .../tfg/sync_service/sync/ProductSyncService.java | 6 ++++-- .../tfg/sync_service/sync/StockSyncService.java | 6 ++++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java index e91997f..f624a0b 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java @@ -8,6 +8,7 @@ import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.client.RestClient; +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @@ -97,7 +98,7 @@ public class PrestashopClient { .queryParam("output_format", "JSON") .build()) .contentType(MediaType.APPLICATION_XML) - .body(toProductXml(dto)) + .body(toProductXml(dto).getBytes(StandardCharsets.UTF_8)) .retrieve() .body(PrestashopProductDto.SingleResponse.class); @@ -113,7 +114,7 @@ public class PrestashopClient { .queryParam("output_format", "JSON") .build(id)) .contentType(MediaType.APPLICATION_XML) - .body(toProductXml(dto)) + .body(toProductXml(dto).getBytes(StandardCharsets.UTF_8)) .retrieve() .toBodilessEntity(); } @@ -152,7 +153,7 @@ public class PrestashopClient { .queryParam("output_format", "JSON") .build(id)) .contentType(MediaType.APPLICATION_XML) - .body(toStockAvailableXml(dto)) + .body(toStockAvailableXml(dto).getBytes(StandardCharsets.UTF_8)) .retrieve() .toBodilessEntity(); } diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/OrderSyncService.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/OrderSyncService.java index b79ad62..13ceee4 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/OrderSyncService.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/OrderSyncService.java @@ -13,6 +13,7 @@ import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.exception.P import com.teterialosjuanjos.tfg.sync_service.mapping.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.stereotype.Service; import org.springframework.web.client.RestClientException; @@ -84,14 +85,15 @@ public class OrderSyncService { try { importOrder(psOrder); processed++; - } catch (DolibarrApiException | PrestashopApiException | RestClientException e) { + } catch (DolibarrApiException | PrestashopApiException + | RestClientException | HttpMessageConversionException e) { failed++; String msg = "PS order %d: %s".formatted(psOrder.id(), e.getMessage()); errors.add(msg); log.warn("OrderSync item failed: {}", msg); } } - } catch (RestClientException e) { + } catch (RuntimeException e) { failed++; String msg = "Failed to fetch orders from PrestaShop: " + e.getMessage(); errors.add(msg); diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/ProductSyncService.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/ProductSyncService.java index 781635d..17908e8 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/ProductSyncService.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/ProductSyncService.java @@ -10,6 +10,7 @@ import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.exception.P import com.teterialosjuanjos.tfg.sync_service.mapping.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.stereotype.Service; import org.springframework.web.client.RestClientException; @@ -69,7 +70,8 @@ public class ProductSyncService { try { pushProduct(product); processed++; - } catch (DolibarrApiException | PrestashopApiException | RestClientException e) { + } catch (DolibarrApiException | PrestashopApiException + | RestClientException | HttpMessageConversionException e) { failed++; String msg = "SKU %s: %s".formatted(product.ref(), e.getMessage()); errors.add(msg); @@ -77,7 +79,7 @@ public class ProductSyncService { markError(product.ref(), e.getMessage()); } } - } catch (RestClientException e) { + } catch (RuntimeException e) { failed++; String msg = "Failed to fetch products from Dolibarr: " + e.getMessage(); errors.add(msg); diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/StockSyncService.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/StockSyncService.java index f85d9e2..0b3dbee 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/StockSyncService.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/sync/StockSyncService.java @@ -9,6 +9,7 @@ import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.exception.P import com.teterialosjuanjos.tfg.sync_service.mapping.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.converter.HttpMessageConversionException; import org.springframework.stereotype.Service; import org.springframework.web.client.RestClientException; @@ -56,14 +57,15 @@ public class StockSyncService { if (pushStock(mapping)) { processed++; } - } catch (DolibarrApiException | PrestashopApiException | RestClientException e) { + } catch (DolibarrApiException | PrestashopApiException + | RestClientException | HttpMessageConversionException e) { failed++; String msg = "SKU %s: %s".formatted(mapping.getSku(), e.getMessage()); errors.add(msg); log.warn("StockSync item failed: {}", msg); } } - } catch (RestClientException e) { + } catch (RuntimeException e) { failed++; String msg = "Infrastructure error: " + e.getMessage(); errors.add(msg);