Skip to content

Commit

Permalink
Do not set exception attribute if response body is set
Browse files Browse the repository at this point in the history
ResponseEntityExceptionHandler should not set the exception attribute
when there is a response body, and the response is fully handled.

Closes gh-31541
  • Loading branch information
rstoyanchev committed Jan 9, 2024
1 parent 23eff5c commit a1463c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -560,14 +560,14 @@ protected ResponseEntity<Object> handleExceptionInternal(
}
}

if (statusCode.equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
}

if (body == null && ex instanceof ErrorResponse errorResponse) {
body = errorResponse.updateAndGetBody(this.messageSource, LocaleContextHolder.getLocale());
}

if (statusCode.equals(HttpStatus.INTERNAL_SERVER_ERROR) && body == null) {
request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
}

return createResponseEntity(body, headers, statusCode, request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ProblemDetail;
Expand Down Expand Up @@ -267,6 +266,15 @@ public void asyncRequestTimeoutException() {
testException(new AsyncRequestTimeoutException());
}

@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();
Expand Down Expand Up @@ -343,11 +351,6 @@ private ResponseEntity<Object> testException(Exception ex) {
try {
ResponseEntity<Object> entity = this.exceptionHandler.handleException(ex, this.request);

// 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());
Expand Down

0 comments on commit a1463c2

Please sign in to comment.