@@ -66,8 +66,6 @@ class RestClientObservationTests {
66
66
67
67
private final ClientHttpResponse response = mock ();
68
68
69
- private final ResponseErrorHandler errorHandler = mock ();
70
-
71
69
private final HttpMessageConverter <String > converter = mock ();
72
70
73
71
private RestClient client ;
@@ -79,7 +77,6 @@ void setupEach() {
79
77
this .client = RestClient .builder ()
80
78
.messageConverters (converters -> converters .add (0 , this .converter ))
81
79
.requestFactory (this .requestFactory )
82
- .defaultStatusHandler (this .errorHandler )
83
80
.observationRegistry (this .observationRegistry )
84
81
.build ();
85
82
this .observationRegistry .observationConfig ().observationHandler (new ContextAssertionObservationHandler ());
@@ -122,6 +119,10 @@ void shouldContributeSuccessOutcome() throws Exception {
122
119
123
120
@ Test
124
121
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
+
125
126
String url = "https://example.org" ;
126
127
mockSentRequest (GET , url );
127
128
mockResponseStatus (HttpStatus .INTERNAL_SERVER_ERROR );
@@ -201,6 +202,19 @@ void shouldAddUnknownContentTypeErrorAsException() throws Exception {
201
202
assertThatHttpObservation ().hasLowCardinalityKeyValue ("exception" , "UnknownContentTypeException" );
202
203
}
203
204
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
+
204
218
@ Test
205
219
void registerObservationWhenReadingBody () throws Exception {
206
220
mockSentRequest (GET , "https://example.org" );
@@ -239,7 +253,6 @@ private void mockSentRequest(HttpMethod method, String uri, HttpHeaders requestH
239
253
240
254
private void mockResponseStatus (HttpStatus responseStatus ) throws Exception {
241
255
given (request .execute ()).willReturn (response );
242
- given (errorHandler .hasError (response )).willReturn (responseStatus .isError ());
243
256
given (response .getStatusCode ()).willReturn (responseStatus );
244
257
given (response .getStatusText ()).willReturn (responseStatus .getReasonPhrase ());
245
258
}
0 commit comments