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 <noreply@anthropic.com>
This commit is contained in:
luklpz 2026-05-14 19:50:03 +02:00
parent 121dc329f8
commit 997ed99895
4 changed files with 16 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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