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 23eff5c650
commit a1463c2bf2
2 changed files with 14 additions and 11 deletions

View File

@@ -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.
@@ -560,14 +560,14 @@ public abstract class ResponseEntityExceptionHandler implements MessageSourceAwa
}
}
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);
}