Enhance PrestashopClient and PrestashopProxyController to add error handling for POST, PUT, and DELETE methods
This commit is contained in:
@@ -2,6 +2,8 @@ package fr.gameovergne.api.controller.prestashop;
|
||||
|
||||
import fr.gameovergne.api.service.prestashop.PrestashopClient;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
@@ -15,6 +17,8 @@ import java.nio.charset.StandardCharsets;
|
||||
@RequestMapping("/api/ps")
|
||||
public class PrestashopProxyController {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(PrestashopProxyController.class);
|
||||
|
||||
private final PrestashopClient prestashopClient;
|
||||
|
||||
public PrestashopProxyController(PrestashopClient prestashopClient) {
|
||||
@@ -68,6 +72,8 @@ public class PrestashopProxyController {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
|
||||
log.info("XML envoyé à Presta:\n{}", xmlBody);
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.postWithRawQuery(path, rawQuery, xmlBody);
|
||||
|
||||
@@ -84,6 +90,8 @@ public class PrestashopProxyController {
|
||||
String path = extractPath(request);
|
||||
String rawQuery = extractDecodedQuery(request);
|
||||
|
||||
log.info("XML envoyé à Presta:\n{}", xmlBody);
|
||||
|
||||
ResponseEntity<String> prestaResponse =
|
||||
prestashopClient.putWithRawQuery(path, rawQuery, xmlBody);
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestClientResponseException;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -124,12 +126,28 @@ public class PrestashopClient {
|
||||
|
||||
log.info("[PrestaShop] POST (proxy) {}", uri);
|
||||
|
||||
try {
|
||||
return client.post()
|
||||
.uri(uri)
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(xmlBody)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
} catch (RestClientResponseException ex) {
|
||||
// On propage tel quel le status + le body XML renvoyé par Presta
|
||||
log.error("[PrestaShop] POST error {} : {}", ex.getRawStatusCode(), ex.getResponseBodyAsString());
|
||||
return ResponseEntity
|
||||
.status(ex.getRawStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(ex.getResponseBodyAsString());
|
||||
} catch (RestClientException ex) {
|
||||
// Cas réseau, timeout, etc.
|
||||
log.error("[PrestaShop] POST technical error", ex);
|
||||
return ResponseEntity
|
||||
.status(502)
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.body("Error while calling PrestaShop WebService");
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<String> putWithRawQuery(String path, String rawQuery, String xmlBody) {
|
||||
@@ -140,12 +158,28 @@ public class PrestashopClient {
|
||||
|
||||
log.info("[PrestaShop] PUT (proxy) {}", uri);
|
||||
|
||||
try {
|
||||
return client.put()
|
||||
.uri(uri)
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(xmlBody)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
} catch (RestClientResponseException ex) {
|
||||
// On propage tel quel le status + le body XML renvoyé par Presta
|
||||
log.error("[PrestaShop] PUT error {} : {}", ex.getRawStatusCode(), ex.getResponseBodyAsString());
|
||||
return ResponseEntity
|
||||
.status(ex.getRawStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(ex.getResponseBodyAsString());
|
||||
} catch (RestClientException ex) {
|
||||
// Cas réseau, timeout, etc.
|
||||
log.error("[PrestaShop] PUT technical error", ex);
|
||||
return ResponseEntity
|
||||
.status(502)
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.body("Error while calling PrestaShop WebService");
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<String> deleteWithRawQuery(String path, String rawQuery) {
|
||||
@@ -156,9 +190,25 @@ public class PrestashopClient {
|
||||
|
||||
log.info("[PrestaShop] DELETE (proxy) {}", uri);
|
||||
|
||||
try {
|
||||
return client.delete()
|
||||
.uri(uri)
|
||||
.retrieve()
|
||||
.toEntity(String.class);
|
||||
} catch (RestClientResponseException ex) {
|
||||
// On propage tel quel le status + le body XML renvoyé par Presta
|
||||
log.error("[PrestaShop] DELETE error {} : {}", ex.getRawStatusCode(), ex.getResponseBodyAsString());
|
||||
return ResponseEntity
|
||||
.status(ex.getRawStatusCode())
|
||||
.contentType(MediaType.APPLICATION_XML)
|
||||
.body(ex.getResponseBodyAsString());
|
||||
} catch (RestClientException ex) {
|
||||
// Cas réseau, timeout, etc.
|
||||
log.error("[PrestaShop] DELETE technical error", ex);
|
||||
return ResponseEntity
|
||||
.status(502)
|
||||
.contentType(MediaType.TEXT_PLAIN)
|
||||
.body("Error while calling PrestaShop WebService");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user