fix: catch RestClientException in all sync services

Per-item catch blocks only handled DolibarrApiException|PrestashopApiException,
letting network-level errors (RestClientException wrapping SSL, timeout, etc.)
propagate uncaught → HTTP 500. Also add outer catch for infrastructure failures
(e.g. getProducts() itself failing) so SyncLog always records the error in
errorDetails and the service returns a valid SyncResult instead of throwing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
luklpz 2026-05-14 19:28:49 +02:00
parent cfce84eb4d
commit 121dc329f8
3 changed files with 21 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import com.teterialosjuanjos.tfg.sync_service.mapping.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import java.time.Instant;
import java.time.LocalDateTime;
@ -83,13 +84,18 @@ public class OrderSyncService {
try {
importOrder(psOrder);
processed++;
} catch (DolibarrApiException | PrestashopApiException e) {
} catch (DolibarrApiException | PrestashopApiException | RestClientException 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) {
failed++;
String msg = "Failed to fetch orders from PrestaShop: " + e.getMessage();
errors.add(msg);
log.error("OrderSync infrastructure error: {}", msg, e);
} finally {
syncLog.setFinishedAt(Instant.now());
syncLog.setItemsProcessed(processed);

View File

@ -11,6 +11,7 @@ import com.teterialosjuanjos.tfg.sync_service.mapping.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import java.time.Instant;
import java.util.ArrayList;
@ -68,7 +69,7 @@ public class ProductSyncService {
try {
pushProduct(product);
processed++;
} catch (DolibarrApiException | PrestashopApiException e) {
} catch (DolibarrApiException | PrestashopApiException | RestClientException e) {
failed++;
String msg = "SKU %s: %s".formatted(product.ref(), e.getMessage());
errors.add(msg);
@ -76,6 +77,11 @@ public class ProductSyncService {
markError(product.ref(), e.getMessage());
}
}
} catch (RestClientException e) {
failed++;
String msg = "Failed to fetch products from Dolibarr: " + e.getMessage();
errors.add(msg);
log.error("ProductSync infrastructure error: {}", msg, e);
} finally {
syncLog.setFinishedAt(Instant.now());
syncLog.setItemsProcessed(processed);

View File

@ -10,6 +10,7 @@ import com.teterialosjuanjos.tfg.sync_service.mapping.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import java.time.Instant;
import java.util.ArrayList;
@ -55,13 +56,18 @@ public class StockSyncService {
if (pushStock(mapping)) {
processed++;
}
} catch (DolibarrApiException | PrestashopApiException e) {
} catch (DolibarrApiException | PrestashopApiException | RestClientException e) {
failed++;
String msg = "SKU %s: %s".formatted(mapping.getSku(), e.getMessage());
errors.add(msg);
log.warn("StockSync item failed: {}", msg);
}
}
} catch (RestClientException e) {
failed++;
String msg = "Infrastructure error: " + e.getMessage();
errors.add(msg);
log.error("StockSync infrastructure error: {}", msg, e);
} finally {
syncLog.setFinishedAt(Instant.now());
syncLog.setItemsProcessed(processed);