Skip to content

feat:bugfix populating response body in case of non 200 status code #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,25 @@ public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientH
} catch (final WebClientResponseException ex) {
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
} catch (final HttpStatusCodeException ex) {
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
ex.getResponseBodyAsString(), ex.getResponseHeaders(), ex.getStatusCode());
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
} catch (final UnknownContentTypeException ex) {
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
// returned in the REST call. The handle went into `RestClientException` which is the parent
// class of `UnknownContentTypeException` and hence some contextual information was lost
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
ex.getResponseBodyAsString(), ex.getResponseHeaders());
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
} catch (final Exception ex) {
final String errorMessage = String
.format("Error in making rest call. Error=%s", ex.getMessage());
return handleException(ex, errorMessage, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
return handleException(ex, errorMessage, null, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
}
}

Expand Down Expand Up @@ -131,11 +134,12 @@ private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(ResponseEntity<
private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
final Exception exception,
final String errorMessage,
final String responseBody,
final HttpStatus httpStatus,
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
log.error("Exception while executing http request for requestUrl={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
httpRequest.getRetryHandlers()
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString()).checkAndThrowRetriableException(exception));
return ClientHttpResponse.<RESPONSE>builder().error(errorMessage).status(httpStatus).build();
return ClientHttpResponse.<RESPONSE>builder().error(responseBody).status(httpStatus).build();
}
}