Support for custom status in ResponseStatusException

Closes gh-20336
This commit is contained in:
Rossen Stoyanchev
2020-07-09 15:06:33 +03:00
parent 4d7418841c
commit 37366e0c91
5 changed files with 68 additions and 29 deletions

View File

@@ -36,7 +36,7 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class ResponseStatusException extends NestedRuntimeException {
private final HttpStatus status;
private final int status;
@Nullable
private final String reason;
@@ -70,15 +70,44 @@ public class ResponseStatusException extends NestedRuntimeException {
public ResponseStatusException(HttpStatus status, @Nullable String reason, @Nullable Throwable cause) {
super(null, cause);
Assert.notNull(status, "HttpStatus is required");
this.status = status;
this.status = status.value();
this.reason = reason;
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param rawStatusCode the HTTP status code value
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
* @since 5.3
*/
public ResponseStatusException(int rawStatusCode, @Nullable String reason, @Nullable Throwable cause) {
super(null, cause);
this.status = rawStatusCode;
this.reason = reason;
}
/**
* Return the HTTP status associated with this exception.
* @throws IllegalArgumentException in case of an unknown HTTP status code
* @since #getRawStatusCode()
* @see HttpStatus#valueOf(int)
*/
public HttpStatus getStatus() {
return HttpStatus.valueOf(this.status);
}
/**
* Return the HTTP status code (potentially non-standard and not resolvable
* through the {@link HttpStatus} enum) as an integer.
* @return the HTTP status as an integer value
* @since 5.3
* @see #getStatus()
* @see HttpStatus#resolve(int)
*/
public int getRawStatusCode() {
return this.status;
}
@@ -121,7 +150,8 @@ public class ResponseStatusException extends NestedRuntimeException {
@Override
public String getMessage() {
String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");
HttpStatus code = HttpStatus.resolve(this.status);
String msg = (code != null ? code : this.status) + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
}

View File

@@ -88,9 +88,10 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
private boolean updateResponse(ServerHttpResponse response, Throwable ex) {
boolean result = false;
HttpStatus status = determineStatus(ex);
if (status != null) {
if (response.setStatusCode(status)) {
HttpStatus httpStatus = determineStatus(ex);
int code = (httpStatus != null ? httpStatus.value() : determineRawStatusCode(ex));
if (code != -1) {
if (response.setRawStatusCode(code)) {
if (ex instanceof ResponseStatusException) {
((ResponseStatusException) ex).getResponseHeaders()
.forEach((name, values) ->
@@ -109,17 +110,30 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler {
}
/**
* Determine the HTTP status implied by the given exception.
* @param ex the exception to introspect
* Determine the HTTP status for the given exception.
* <p>As of 5.3 this method always returns {@code null} in which case
* {@link #determineRawStatusCode(Throwable)} is used instead.
* @param ex the exception to check
* @return the associated HTTP status, if any
* @since 5.0.5
* @deprecated as of 5.3 in favor of {@link #determineRawStatusCode(Throwable)}.
*/
@Nullable
@Deprecated
protected HttpStatus determineStatus(Throwable ex) {
if (ex instanceof ResponseStatusException) {
return ((ResponseStatusException) ex).getStatus();
}
return null;
}
/**
* Determine the raw status code for the given exception.
* @param ex the exception to check
* @return the associated HTTP status code, or -1 if it can't be derived.
* @since 5.3
*/
protected int determineRawStatusCode(Throwable ex) {
if (ex instanceof ResponseStatusException) {
return ((ResponseStatusException) ex).getRawStatusCode();
}
return -1;
}
}