|
39 | 39 | import org.springframework.http.client.ClientHttpResponse;
|
40 | 40 | import org.springframework.http.client.observation.ClientRequestObservationContext;
|
41 | 41 | import org.springframework.http.converter.HttpMessageConverter;
|
| 42 | +import org.springframework.lang.Nullable; |
42 | 43 |
|
43 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 45 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 46 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
45 | 47 | import static org.mockito.ArgumentMatchers.any;
|
46 | 48 | import static org.mockito.ArgumentMatchers.eq;
|
47 | 49 | import static org.mockito.BDDMockito.given;
|
@@ -158,6 +160,31 @@ void executeWithIoExceptionAddsUnknownOutcome() throws Exception {
|
158 | 160 | assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "UNKNOWN");
|
159 | 161 | }
|
160 | 162 |
|
| 163 | + @Test // gh-32060 |
| 164 | + void executeShouldRecordErrorsThrownByErrorHandler() throws Exception { |
| 165 | + mockSentRequest(GET, "https://example.org"); |
| 166 | + mockResponseStatus(HttpStatus.OK); |
| 167 | + mockResponseBody("Hello World", MediaType.TEXT_PLAIN); |
| 168 | + given(errorHandler.hasError(any())).willThrow(new IllegalStateException("error handler")); |
| 169 | + |
| 170 | + assertThatIllegalStateException().isThrownBy(() -> |
| 171 | + template.execute("https://example.org", GET, null, null)); |
| 172 | + |
| 173 | + assertThatHttpObservation().hasLowCardinalityKeyValue("exception", "IllegalStateException"); |
| 174 | + } |
| 175 | + |
| 176 | + @Test // gh-32060 |
| 177 | + void executeShouldCreateObservationScope() throws Exception { |
| 178 | + mockSentRequest(GET, "https://example.org"); |
| 179 | + mockResponseStatus(HttpStatus.OK); |
| 180 | + mockResponseBody("Hello World", MediaType.TEXT_PLAIN); |
| 181 | + ObservationErrorHandler observationErrorHandler = new ObservationErrorHandler(observationRegistry); |
| 182 | + template.setErrorHandler(observationErrorHandler); |
| 183 | + |
| 184 | + template.execute("https://example.org", GET, null, null); |
| 185 | + assertThat(observationErrorHandler.currentObservation).isNotNull(); |
| 186 | + } |
| 187 | + |
161 | 188 |
|
162 | 189 | private void mockSentRequest(HttpMethod method, String uri) throws Exception {
|
163 | 190 | mockSentRequest(method, uri, new HttpHeaders());
|
@@ -205,4 +232,26 @@ public void onStart(ClientRequestObservationContext context) {
|
205 | 232 | }
|
206 | 233 | }
|
207 | 234 |
|
| 235 | + static class ObservationErrorHandler implements ResponseErrorHandler { |
| 236 | + |
| 237 | + final TestObservationRegistry observationRegistry; |
| 238 | + |
| 239 | + @Nullable |
| 240 | + Observation currentObservation; |
| 241 | + |
| 242 | + public ObservationErrorHandler(TestObservationRegistry observationRegistry) { |
| 243 | + this.observationRegistry = observationRegistry; |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public boolean hasError(ClientHttpResponse response) throws IOException { |
| 248 | + return true; |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void handleError(ClientHttpResponse response) throws IOException { |
| 253 | + currentObservation = this.observationRegistry.getCurrentObservation(); |
| 254 | + } |
| 255 | + } |
| 256 | + |
208 | 257 | }
|
0 commit comments