Skip to content

Commit

Permalink
fix tokenizer error response mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
giomella committed Nov 9, 2023
1 parent 3c76bf0 commit 2bd39ad
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,28 @@ public String generateTokenForFiscalCode(String fiscalCode) throws PDVTokenizerE

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

private void handleErrorResponse(HttpResponse<String> 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());
Expand Down

0 comments on commit 2bd39ad

Please sign in to comment.