Assuming 500 status code when an exception was thrown (#1889)

* Assuming 500 status code when an exception was thrown in the controller; fixes gh-1880

* Ensures that the response is committed
This commit is contained in:
Marcin Grzejszczak
2021-03-30 13:22:35 +02:00
committed by GitHub
parent 28d8802f4a
commit 5ab1c13f0c
2 changed files with 22 additions and 2 deletions

View File

@@ -415,8 +415,11 @@ public class TraceWebFilter implements WebFilter, Ordered, ApplicationContextAwa
@Override
public int statusCode() {
if (this.throwable != null && this.throwable instanceof ResponseStatusException) {
return ((ResponseStatusException) this.throwable).getRawStatusCode();
if (!this.delegate.isCommitted() && this.throwable != null) {
if (this.throwable instanceof ResponseStatusException) {
return ((ResponseStatusException) this.throwable).getRawStatusCode();
}
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
HttpStatus statusCode = this.delegate.getStatusCode();
return statusCode != null ? statusCode.value() : 0;

View File

@@ -83,6 +83,12 @@ public class TraceWebFluxTests {
thenSpanWith404StatusCodeWasReported(spans, response);
spans.clear();
// when
response = whenRequestIsSent(port, "/exception");
// then
thenSpanWithExceptionWasReported(spans, response);
spans.clear();
// when
ClientResponse nonSampledResponse = whenNonSampledRequestIsSent(port);
// then
@@ -137,6 +143,12 @@ public class TraceWebFluxTests {
then(spans.get(0).tags()).hasEntrySatisfying("http.status_code", value -> then(value).isEqualTo("404"));
}
private void thenSpanWithExceptionWasReported(TestSpanHandler spans, ClientResponse response) {
Awaitility.await().untilAsserted(() -> then(response.statusCode().value()).isEqualTo(500));
then(spans).hasSize(1);
then(spans.get(0).tags()).hasEntrySatisfying("http.status_code", value -> then(value).isEqualTo("500"));
}
private void thenNoSpanWasReported(TestSpanHandler spans, ClientResponse response, Controller2 controller2) {
Awaitility.await().untilAsserted(() -> {
then(response.statusCode().value()).isEqualTo(200);
@@ -229,6 +241,11 @@ public class TraceWebFluxTests {
return Flux.just(sampled.toString());
}
@GetMapping("/exception")
public Flux<Void> exception() {
throw new RuntimeException("Exception");
}
}
}