fix: handle Dolibarr/PS 404 on empty filter results
Both APIs return 404 (not 200+empty) when a filter finds no results. Catch DolibarrApiException(404) in getOrCreateThirdparty and getProductByRef; catch PrestashopApiException(404) in getProductByReference — treat as empty/not-found instead of error. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
fa20b7cc91
commit
db4db1741d
|
|
@ -1,6 +1,7 @@
|
||||||
package com.teterialosjuanjos.tfg.sync_service.integration.dolibarr;
|
package com.teterialosjuanjos.tfg.sync_service.integration.dolibarr;
|
||||||
|
|
||||||
import com.teterialosjuanjos.tfg.sync_service.integration.dolibarr.dto.*;
|
import com.teterialosjuanjos.tfg.sync_service.integration.dolibarr.dto.*;
|
||||||
|
import com.teterialosjuanjos.tfg.sync_service.integration.dolibarr.exception.DolibarrApiException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
|
@ -50,15 +51,19 @@ public class DolibarrClient {
|
||||||
*/
|
*/
|
||||||
public DolibarrProductDto getProductByRef(String ref) {
|
public DolibarrProductDto getProductByRef(String ref) {
|
||||||
String filter = "(t.ref:=:'%s')".formatted(ref);
|
String filter = "(t.ref:=:'%s')".formatted(ref);
|
||||||
List<DolibarrProductDto> results = restClient.get()
|
try {
|
||||||
.uri(u -> u.path("/products")
|
List<DolibarrProductDto> results = restClient.get()
|
||||||
.queryParam("sqlfilters", filter)
|
.uri(u -> u.path("/products")
|
||||||
.queryParam("limit", 1)
|
.queryParam("sqlfilters", filter)
|
||||||
.build())
|
.queryParam("limit", 1)
|
||||||
.retrieve()
|
.build())
|
||||||
.body(new ParameterizedTypeReference<List<DolibarrProductDto>>() {});
|
.retrieve()
|
||||||
|
.body(new ParameterizedTypeReference<List<DolibarrProductDto>>() {});
|
||||||
return (results != null && !results.isEmpty()) ? results.get(0) : null;
|
return (results != null && !results.isEmpty()) ? results.get(0) : null;
|
||||||
|
} catch (DolibarrApiException e) {
|
||||||
|
if (e.getStatusCode().value() == 404) return null;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -119,13 +124,21 @@ public class DolibarrClient {
|
||||||
*/
|
*/
|
||||||
public DolibarrThirdpartyDto getOrCreateThirdparty(String email, String name) {
|
public DolibarrThirdpartyDto getOrCreateThirdparty(String email, String name) {
|
||||||
log.debug("Looking up Dolibarr thirdparty email={}", email);
|
log.debug("Looking up Dolibarr thirdparty email={}", email);
|
||||||
List<DolibarrThirdpartyDto> matches = restClient.get()
|
List<DolibarrThirdpartyDto> matches = null;
|
||||||
.uri(u -> u.path("/thirdparties")
|
try {
|
||||||
.queryParam("sqlfilters", "(t.email:=:'%s')".formatted(email))
|
matches = restClient.get()
|
||||||
.queryParam("limit", 1)
|
.uri(u -> u.path("/thirdparties")
|
||||||
.build())
|
.queryParam("sqlfilters", "(t.email:=:'%s')".formatted(email))
|
||||||
.retrieve()
|
.queryParam("limit", 1)
|
||||||
.body(new ParameterizedTypeReference<List<DolibarrThirdpartyDto>>() {});
|
.build())
|
||||||
|
.retrieve()
|
||||||
|
.body(new ParameterizedTypeReference<List<DolibarrThirdpartyDto>>() {});
|
||||||
|
} catch (DolibarrApiException e) {
|
||||||
|
// Dolibarr returns 404 (not 200 + empty array) when filter finds no results
|
||||||
|
if (e.getStatusCode().value() != 404) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (matches != null && !matches.isEmpty()) {
|
if (matches != null && !matches.isEmpty()) {
|
||||||
return matches.get(0);
|
return matches.get(0);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.teterialosjuanjos.tfg.sync_service.integration.prestashop;
|
||||||
|
|
||||||
import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties;
|
import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties;
|
||||||
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*;
|
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*;
|
||||||
|
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.exception.PrestashopApiException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
|
@ -66,20 +67,26 @@ public class PrestashopClient {
|
||||||
public Optional<PrestashopProductDto> getProductByReference(String reference) {
|
public Optional<PrestashopProductDto> getProductByReference(String reference) {
|
||||||
// PS WebService requires [value] brackets for exact-match filtering
|
// PS WebService requires [value] brackets for exact-match filtering
|
||||||
String exactRef = "[" + reference + "]";
|
String exactRef = "[" + reference + "]";
|
||||||
PrestashopProductDto.ListResponse response = restClient.get()
|
try {
|
||||||
.uri(u -> u.path("/products")
|
PrestashopProductDto.ListResponse response = restClient.get()
|
||||||
.queryParam("ws_key", wsKey)
|
.uri(u -> u.path("/products")
|
||||||
.queryParam("output_format", "JSON")
|
.queryParam("ws_key", wsKey)
|
||||||
.queryParam("display", "full")
|
.queryParam("output_format", "JSON")
|
||||||
.queryParam("filter[reference]", exactRef)
|
.queryParam("display", "full")
|
||||||
.build())
|
.queryParam("filter[reference]", exactRef)
|
||||||
.retrieve()
|
.build())
|
||||||
.body(PrestashopProductDto.ListResponse.class);
|
.retrieve()
|
||||||
|
.body(PrestashopProductDto.ListResponse.class);
|
||||||
|
|
||||||
if (response == null || response.products() == null || response.products().isEmpty()) {
|
if (response == null || response.products() == null || response.products().isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
return Optional.of(response.products().get(0));
|
||||||
|
} catch (PrestashopApiException e) {
|
||||||
|
// PS may return 404 instead of 200+empty when filter finds no results
|
||||||
|
if (e.getStatusCode() != null && e.getStatusCode().value() == 404) return Optional.empty();
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return Optional.of(response.products().get(0));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue