BadRequestStatusException -> ServerWebInputException

The renaming makes it clear this exception is for use where 400 error
applies within a Spring web application where the error may be
associated with a MethodParameter, a BindingResult, and so on.

There is no need for BadRequestStatusException which can be expressed
with ResponseStatusException(HttpStatus.BAD_REQUEST, "reason").
This commit is contained in:
Rossen Stoyanchev 2016-04-20 13:31:46 -04:00
parent 54eeb2cdb1
commit dd607d3e53
3 changed files with 12 additions and 10 deletions

View File

@ -43,7 +43,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.result.condition.ParamsRequestCondition; import org.springframework.web.reactive.result.condition.ParamsRequestCondition;
import org.springframework.web.server.BadRequestStatusException; import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.MethodNotAllowedException; import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
@ -203,7 +203,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
* and HTTP method but not by consumable media types * and HTTP method but not by consumable media types
* @throws NotAcceptableStatusException if there are matches by URL and HTTP * @throws NotAcceptableStatusException if there are matches by URL and HTTP
* method but not by producible media types * method but not by producible media types
* @throws BadRequestStatusException if there are matches by URL and HTTP * @throws ServerWebInputException if there are matches by URL and HTTP
* method but not by query parameter conditions * method but not by query parameter conditions
*/ */
@Override @Override
@ -278,7 +278,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
.collect(Collectors.toMap(Entry::getKey, .collect(Collectors.toMap(Entry::getKey,
entry -> entry.getValue().toArray(new String[entry.getValue().size()])) entry -> entry.getValue().toArray(new String[entry.getValue().size()]))
); );
throw new BadRequestStatusException("Unsatisfied query parameter conditions: " + throw new ServerWebInputException("Unsatisfied query parameter conditions: " +
paramConditions + ", actual parameters: " + params); paramConditions + ", actual parameters: " + params);
} }
else { else {

View File

@ -21,11 +21,13 @@ import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
/** /**
* Exception for errors that fit response status 400 (bad request). * Exception for errors that fit response status 400 (bad request) for use in
* Spring Web applications. The exception provides additional fields (e.g.
* an optional {@link MethodParameter} if related to the error).
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class BadRequestStatusException extends ResponseStatusException { public class ServerWebInputException extends ResponseStatusException {
private final MethodParameter parameter; private final MethodParameter parameter;
@ -33,21 +35,21 @@ public class BadRequestStatusException extends ResponseStatusException {
/** /**
* Constructor with an explanation only. * Constructor with an explanation only.
*/ */
public BadRequestStatusException(String reason) { public ServerWebInputException(String reason) {
this(reason, null); this(reason, null);
} }
/** /**
* Constructor for a 400 error linked to a specific {@code MethodParameter}. * Constructor for a 400 error linked to a specific {@code MethodParameter}.
*/ */
public BadRequestStatusException(String reason, MethodParameter parameter) { public ServerWebInputException(String reason, MethodParameter parameter) {
this(reason, parameter, null); this(reason, parameter, null);
} }
/** /**
* Constructor for a 400 error with a root cause. * Constructor for a 400 error with a root cause.
*/ */
public BadRequestStatusException(String reason, MethodParameter parameter, Throwable cause) { public ServerWebInputException(String reason, MethodParameter parameter, Throwable cause) {
super(HttpStatus.BAD_REQUEST, reason, cause); super(HttpStatus.BAD_REQUEST, reason, cause);
this.parameter = parameter; this.parameter = parameter;
} }

View File

@ -50,7 +50,7 @@ import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.HandlerResult; import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.reactive.result.method.RequestMappingInfo.BuilderConfiguration; import org.springframework.web.reactive.result.method.RequestMappingInfo.BuilderConfiguration;
import org.springframework.web.server.BadRequestStatusException; import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.MethodNotAllowedException; import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.NotAcceptableStatusException; import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
@ -208,7 +208,7 @@ public class RequestMappingInfoHandlerMappingTests {
public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception { public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception {
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/params"); ServerWebExchange exchange = createExchange(HttpMethod.GET, "/params");
Mono<Object> mono = this.handlerMapping.getHandler(exchange); Mono<Object> mono = this.handlerMapping.getHandler(exchange);
assertError(mono, BadRequestStatusException.class, ex -> { assertError(mono, ServerWebInputException.class, ex -> {
assertEquals(ex.getReason(), "Unsatisfied query parameter conditions: " + assertEquals(ex.getReason(), "Unsatisfied query parameter conditions: " +
"[[bar=baz], [foo=bar]], actual parameters: {}"); "[[bar=baz], [foo=bar]], actual parameters: {}");
}); });