ResponseStatusException reason as message code for ProblemDetail
See gh-30300
This commit is contained in:
parent
6697f01d05
commit
a3532bfccc
|
|
@ -40,6 +40,7 @@ import org.springframework.lang.Nullable;
|
|||
* {@code @RestController} or {@code RestControllerAdvice} class.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Yanming Zhou
|
||||
* @since 6.0
|
||||
* @see ErrorResponseException
|
||||
*/
|
||||
|
|
@ -142,6 +143,14 @@ public interface ErrorResponse {
|
|||
if (detail != null) {
|
||||
getBody().setDetail(detail);
|
||||
}
|
||||
else {
|
||||
// detail from ResponseStatusException reason may be message code
|
||||
detail = getBody().getDetail();
|
||||
if (detail != null) {
|
||||
detail = messageSource.getMessage(detail, null, detail, locale);
|
||||
getBody().setDetail(detail);
|
||||
}
|
||||
}
|
||||
String title = messageSource.getMessage(getTitleMessageCode(), null, null, locale);
|
||||
if (title != null) {
|
||||
getBody().setTitle(title);
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
|
|||
import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
|
@ -80,6 +81,7 @@ import static org.mockito.BDDMockito.mock;
|
|||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
public class ResponseEntityExceptionHandlerTests {
|
||||
|
||||
|
|
@ -199,6 +201,30 @@ public class ResponseEntityExceptionHandlerTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reasonAsDetailShouldBeUpdatedViaMessageSource() {
|
||||
|
||||
Locale locale = Locale.UK;
|
||||
LocaleContextHolder.setLocale(locale);
|
||||
|
||||
String code = "bad.request";
|
||||
String message = "Breaking Bad Request";
|
||||
try {
|
||||
StaticMessageSource messageSource = new StaticMessageSource();
|
||||
messageSource.addMessage(code, locale, message);
|
||||
|
||||
this.exceptionHandler.setMessageSource(messageSource);
|
||||
|
||||
ResponseEntity<?> entity = testException(new ResponseStatusException(HttpStatus.BAD_REQUEST, code));
|
||||
|
||||
ProblemDetail body = (ProblemDetail) entity.getBody();
|
||||
assertThat(body.getDetail()).isEqualTo(message);
|
||||
}
|
||||
finally {
|
||||
LocaleContextHolder.resetLocaleContext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conversionNotSupported() {
|
||||
testException(new ConversionNotSupportedException(new Object(), Object.class, null));
|
||||
|
|
|
|||
Loading…
Reference in New Issue