ResponseStatusException delegates to protected constructor

This ensures that by default the reason is used to set the "detail"
field. It's a follow-up fix to a27f2e994b
which resolved the issue partially.

Closes gh-29608
This commit is contained in:
Baljit Singh 2022-11-29 08:32:24 -05:00 committed by rstoyanchev
parent 955ca4d146
commit b1fdb148d0
4 changed files with 7 additions and 7 deletions

View File

@ -56,7 +56,9 @@ public class MethodNotAllowedException extends ResponseStatusException {
} }
this.method = method; this.method = method;
this.httpMethods = Collections.unmodifiableSet(new LinkedHashSet<>(supportedMethods)); this.httpMethods = Collections.unmodifiableSet(new LinkedHashSet<>(supportedMethods));
setDetail(this.httpMethods.isEmpty() ? getReason() : "Supported methods: " + this.httpMethods); if (!this.httpMethods.isEmpty()) {
setDetail("Supported methods: " + this.httpMethods);
}
} }

View File

@ -42,7 +42,6 @@ public class MissingRequestValueException extends ServerWebInputException {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.label = label; this.label = label;
setDetail(getReason());
} }

View File

@ -76,8 +76,7 @@ public class ResponseStatusException extends ErrorResponseException {
* @param cause a nested exception (optional) * @param cause a nested exception (optional)
*/ */
public ResponseStatusException(HttpStatusCode status, @Nullable String reason, @Nullable Throwable cause) { public ResponseStatusException(HttpStatusCode status, @Nullable String reason, @Nullable Throwable cause) {
super(status, cause); this(status, reason, cause, null, null);
this.reason = reason;
} }
/** /**

View File

@ -111,7 +111,7 @@ public class ResponseStatusExceptionResolverTests {
} }
@Test // SPR-12903 @Test // SPR-12903
public void nestedException() throws Exception { public void nestedException() {
Exception cause = new StatusCodeAndReasonMessageException(); Exception cause = new StatusCodeAndReasonMessageException();
TypeMismatchException ex = new TypeMismatchException("value", ITestBean.class, cause); TypeMismatchException ex = new TypeMismatchException("value", ITestBean.class, cause);
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
@ -119,14 +119,14 @@ public class ResponseStatusExceptionResolverTests {
} }
@Test @Test
public void responseStatusException() throws Exception { public void responseStatusException() {
ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST); ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST);
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertResolved(mav, 400, null); assertResolved(mav, 400, null);
} }
@Test // SPR-15524 @Test // SPR-15524
public void responseStatusExceptionWithReason() throws Exception { public void responseStatusExceptionWithReason() {
ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "The reason"); ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "The reason");
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertResolved(mav, 400, "The reason"); assertResolved(mav, 400, "The reason");