Skip to content

Commit 4c444bd

Browse files
jatin14493jmahajan
andauthored
feat: fix Logging Improvements in Spring WebClient calls (#19)
Co-authored-by: jmahajan <jatin_mahajan@intuit.com>
1 parent 96da976 commit 4c444bd

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

src/main/java/com/intuit/springwebclient/client/CommonSpringWebClient.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,31 @@ public class CommonSpringWebClient {
4141
*/
4242
public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
4343
try {
44-
log.info("Executing http request for request={}, method={}", httpRequest.getUrl(),
44+
log.info("Executing http request for request={}, method={}", httpRequest.getRequest(),
4545
httpRequest.getHttpMethod());
46-
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType()).map(resp -> {
47-
return generateResponse(resp);
48-
}).retryWhen(generateRetrySpec(httpRequest)).block();
49-
}
50-
catch (final WebClientResponseException ex) {
46+
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType())
47+
.map(this::generateResponse).retryWhen(generateRetrySpec(httpRequest)).block();
48+
} catch (final WebClientResponseException ex) {
5149
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
5250
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
5351
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
52+
} catch (final HttpStatusCodeException ex) {
53+
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
54+
ex.getResponseBodyAsString(), ex.getResponseHeaders(), ex.getStatusCode());
55+
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
56+
} catch (final UnknownContentTypeException ex) {
57+
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
58+
// returned in the REST call. The handle went into `RestClientException` which is the parent
59+
// class of `UnknownContentTypeException` and hence some contextual information was lost
60+
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
61+
ex.getResponseBodyAsString(), ex.getResponseHeaders());
62+
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
63+
} catch (final Exception ex) {
64+
final String errorMessage = String
65+
.format("Error in making rest call. Error=%s", ex.getMessage());
66+
return handleException(ex, errorMessage, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
5467
}
55-
catch (final HttpStatusCodeException ex) {
56-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
57-
ex.getResponseBodyAsString(), ex.getResponseHeaders(),ex.getStatusCode());
58-
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
59-
} catch (final UnknownContentTypeException ex) {
60-
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
61-
// returned in the REST call. The handle went into `RestClientException` which is the parent
62-
// class of `UnknownContentTypeException` and hence some contextual information was lost
63-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
64-
ex.getResponseBodyAsString(), ex.getResponseHeaders());
65-
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
66-
} catch (final Exception ex) {
67-
final String errorMessage = String
68-
.format("Error in making rest call. Error=%s", ex.getMessage());
69-
return handleException(ex, errorMessage, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
70-
}
71-
}
68+
}
7269

7370
/**
7471
* Generate Web Client Response spec from http request.
@@ -102,7 +99,7 @@ private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, R
10299
return Retry
103100
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
104101
Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
105-
.doBeforeRetry(signal -> log.info("Retrying for request={}, retryCount={}", httpRequest.getUrl(),
102+
.doBeforeRetry(signal -> log.info("Retrying for requestUrl={}, retryCount={}", httpRequest.getUrl(),
106103
signal.totalRetries()))
107104
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
108105
}
@@ -133,7 +130,7 @@ private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
133130
final String errorMessage,
134131
final HttpStatus httpStatus,
135132
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
136-
log.error("Exception while executing http request for request={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
133+
log.error("Exception while executing http request for requestUrl={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
137134
httpRequest.getRetryHandlers()
138135
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString()).checkAndThrowRetriableException(exception));
139136
return ClientHttpResponse.<RESPONSE>builder().error(errorMessage).status(httpStatus).build();

src/main/java/com/intuit/springwebclient/entity/ClientHttpResponse.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,4 @@ public final class ClientHttpResponse<T>{
1313
private final HttpStatus status;
1414
private final boolean isSuccess2xx;
1515

16-
public int statusCode() {
17-
return this.status.value();
18-
}
19-
20-
public boolean isSuccess2xx() {
21-
return this.isSuccess2xx;
22-
}
2316
}

0 commit comments

Comments
 (0)