diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/PDVTokenizerServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/PDVTokenizerServiceImpl.java index 3a5a95b0..89a2e0f6 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/PDVTokenizerServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/PDVTokenizerServiceImpl.java @@ -75,7 +75,19 @@ public String generateTokenForFiscalCode(String fiscalCode) throws PDVTokenizerE HttpResponse httpResponse = pdvTokenizerClient.createToken(tokenizerBody); - handleErrorResponse(httpResponse, "generateTokenForFiscalCode"); + if (httpResponse.statusCode() == HttpStatus.SC_BAD_REQUEST + || httpResponse.statusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) { + ErrorResponse response = ObjectMapperUtils.mapString(httpResponse.body(), ErrorResponse.class); + String errMsg = String.format("PDV Tokenizer generateTokenForFiscalCode invocation failed with status %s and message: %s. Error description: %s (%s)", + response.getStatus(), response.getTitle(), response.getDetail(), response.getType()); + throw new PDVTokenizerException(errMsg, response.getStatus()); + } + if (httpResponse.statusCode() != HttpStatus.SC_OK) { + ErrorMessage response = ObjectMapperUtils.mapString(httpResponse.body(), ErrorMessage.class); + String errMsg = String.format("PDV Tokenizer generateTokenForFiscalCode invocation failed with status %s and message: %s.", + httpResponse.statusCode(), response.getMessage()); + throw new PDVTokenizerException(errMsg, httpResponse.statusCode()); + } TokenResource tokenResource = ObjectMapperUtils.mapString(httpResponse.body(), TokenResource.class); logger.debug("PDV Tokenizer generateTokenForFiscalCode invocation completed"); return tokenResource.getToken(); @@ -83,7 +95,8 @@ public String generateTokenForFiscalCode(String fiscalCode) throws PDVTokenizerE private void handleErrorResponse(HttpResponse httpResponse, String serviceName) throws JsonProcessingException, PDVTokenizerException { if (httpResponse.statusCode() == HttpStatus.SC_BAD_REQUEST - || httpResponse.statusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR) { + || httpResponse.statusCode() == HttpStatus.SC_INTERNAL_SERVER_ERROR + || httpResponse.statusCode() == HttpStatus.SC_NOT_FOUND) { ErrorResponse response = ObjectMapperUtils.mapString(httpResponse.body(), ErrorResponse.class); String errMsg = String.format("PDV Tokenizer %s invocation failed with status %s and message: %s. Error description: %s (%s)", serviceName, response.getStatus(), response.getTitle(), response.getDetail(), response.getType());