Skip to content

Commit c80bf57

Browse files
committed
Merge branch '6.1.x'
2 parents 80fb8ea + 5aa576f commit c80bf57

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ private <T> T readWithMessageConverters(ClientHttpResponse clientResponse, Runna
232232
}
233233
throw unknownContentTypeException;
234234
}
235-
catch (UncheckedIOException | IOException | HttpMessageNotReadableException ex) {
235+
catch (UncheckedIOException | IOException | HttpMessageNotReadableException exc) {
236236
Throwable cause;
237-
if (ex instanceof UncheckedIOException uncheckedIOException) {
237+
if (exc instanceof UncheckedIOException uncheckedIOException) {
238238
cause = uncheckedIOException.getCause();
239239
}
240240
else {
241-
cause = ex;
241+
cause = exc;
242242
}
243243
RestClientException restClientException = new RestClientException("Error while extracting response for type [" +
244244
ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause);
@@ -247,6 +247,12 @@ private <T> T readWithMessageConverters(ClientHttpResponse clientResponse, Runna
247247
}
248248
throw restClientException;
249249
}
250+
catch (RestClientException restClientException) {
251+
if (observation != null) {
252+
observation.error(restClientException);
253+
}
254+
throw restClientException;
255+
}
250256
finally {
251257
if (observation != null) {
252258
observation.stop();

spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class RestClientObservationTests {
6666

6767
private final ClientHttpResponse response = mock();
6868

69-
private final ResponseErrorHandler errorHandler = mock();
70-
7169
private final HttpMessageConverter<String> converter = mock();
7270

7371
private RestClient client;
@@ -79,7 +77,6 @@ void setupEach() {
7977
this.client = RestClient.builder()
8078
.messageConverters(converters -> converters.add(0, this.converter))
8179
.requestFactory(this.requestFactory)
82-
.defaultStatusHandler(this.errorHandler)
8380
.observationRegistry(this.observationRegistry)
8481
.build();
8582
this.observationRegistry.observationConfig().observationHandler(new ContextAssertionObservationHandler());
@@ -122,6 +119,10 @@ void shouldContributeSuccessOutcome() throws Exception {
122119

123120
@Test
124121
void shouldContributeServerErrorOutcome() throws Exception {
122+
ResponseErrorHandler errorHandler = mock();
123+
given(errorHandler.hasError(response)).willReturn(true);
124+
this.client = this.client.mutate().defaultStatusHandler(errorHandler).build();
125+
125126
String url = "https://example.org";
126127
mockSentRequest(GET, url);
127128
mockResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR);
@@ -201,6 +202,19 @@ void shouldAddUnknownContentTypeErrorAsException() throws Exception {
201202
assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "UnknownContentTypeException");
202203
}
203204

205+
@Test
206+
void shouldAddRestClientExceptionAsError() throws Exception {
207+
String url = "https://example.org";
208+
mockSentRequest(GET, url);
209+
mockResponseStatus(HttpStatus.NOT_FOUND);
210+
mockResponseBody("Not Found", MediaType.TEXT_HTML);
211+
212+
assertThatExceptionOfType(RestClientException.class).isThrownBy(() ->
213+
client.get().uri(url).retrieve().toEntity(String.class));
214+
215+
assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "NotFound");
216+
}
217+
204218
@Test
205219
void registerObservationWhenReadingBody() throws Exception {
206220
mockSentRequest(GET, "https://example.org");
@@ -239,7 +253,6 @@ private void mockSentRequest(HttpMethod method, String uri, HttpHeaders requestH
239253

240254
private void mockResponseStatus(HttpStatus responseStatus) throws Exception {
241255
given(request.execute()).willReturn(response);
242-
given(errorHandler.hasError(response)).willReturn(responseStatus.isError());
243256
given(response.getStatusCode()).willReturn(responseStatus);
244257
given(response.getStatusText()).willReturn(responseStatus.getReasonPhrase());
245258
}

0 commit comments

Comments
 (0)