Skip to content

Commit 2593b60

Browse files
committed
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
1 parent e6f6381 commit 2593b60

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -658,14 +658,14 @@ protected ResponseEntity<Object> handleExceptionInternal(
658658
}
659659
}
660660

661-
if (statusCode.equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
662-
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
663-
}
664-
665661
if (body == null && ex instanceof ErrorResponse errorResponse) {
666662
body = errorResponse.updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
667663
}
668664

665+
if (statusCode.equals(HttpStatus.INTERNAL_SERVER_ERROR) && body == null) {
666+
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
667+
}
668+
669669
return createResponseEntity(body, headers, statusCode, request);
670670
}
671671

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ public void maxUploadSizeExceededException() {
323323
testException(new MaxUploadSizeExceededException(1000));
324324
}
325325

326+
@Test // gh-14287, gh-31541
327+
void serverErrorWithoutBody() {
328+
HttpStatusCode code = HttpStatusCode.valueOf(500);
329+
Exception ex = new IllegalStateException("internal error");
330+
this.exceptionHandler.handleExceptionInternal(ex, null, new HttpHeaders(), code, this.request);
331+
332+
assertThat(this.servletRequest.getAttribute("jakarta.servlet.error.exception")).isSameAs(ex);
333+
}
334+
326335
@Test
327336
public void controllerAdvice() throws Exception {
328337
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
@@ -400,11 +409,6 @@ private ResponseEntity<Object> testException(Exception ex) {
400409
ResponseEntity<Object> entity = this.exceptionHandler.handleException(ex, this.request);
401410
assertThat(entity).isNotNull();
402411

403-
// SPR-9653
404-
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(entity.getStatusCode())) {
405-
assertThat(this.servletRequest.getAttribute("jakarta.servlet.error.exception")).isSameAs(ex);
406-
}
407-
408412
// Verify DefaultHandlerExceptionResolver would set the same status
409413
this.exceptionResolver.resolveException(this.servletRequest, this.servletResponse, null, ex);
410414
assertThat(entity.getStatusCode().value()).isEqualTo(this.servletResponse.getStatus());

0 commit comments

Comments
 (0)