Do not set exception attribute if response body is set

ResponseEntityExceptionHandler should not set the exception attribute
when there is a response body, and the response is fully handled.

Closes gh-31541
This commit is contained in:
rstoyanchev
2024-01-09 12:09:54 +00:00
parent e6f638132c
commit 2593b60f2b
2 changed files with 14 additions and 10 deletions

View File

@@ -323,6 +323,15 @@ public class ResponseEntityExceptionHandlerTests {
testException(new MaxUploadSizeExceededException(1000));
}
@Test // gh-14287, gh-31541
void serverErrorWithoutBody() {
HttpStatusCode code = HttpStatusCode.valueOf(500);
Exception ex = new IllegalStateException("internal error");
this.exceptionHandler.handleExceptionInternal(ex, null, new HttpHeaders(), code, this.request);
assertThat(this.servletRequest.getAttribute("jakarta.servlet.error.exception")).isSameAs(ex);
}
@Test
public void controllerAdvice() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
@@ -400,11 +409,6 @@ public class ResponseEntityExceptionHandlerTests {
ResponseEntity<Object> entity = this.exceptionHandler.handleException(ex, this.request);
assertThat(entity).isNotNull();
// SPR-9653
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(entity.getStatusCode())) {
assertThat(this.servletRequest.getAttribute("jakarta.servlet.error.exception")).isSameAs(ex);
}
// Verify DefaultHandlerExceptionResolver would set the same status
this.exceptionResolver.resolveException(this.servletRequest, this.servletResponse, null, ex);
assertThat(entity.getStatusCode().value()).isEqualTo(this.servletResponse.getStatus());