diff --git a/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java b/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java deleted file mode 100644 index 3aa9eac84f4..00000000000 --- a/spring-web-reactive/src/main/java/org/springframework/web/ResponseStatusException.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2002-2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.web; - -import org.springframework.core.NestedRuntimeException; -import org.springframework.http.HttpStatus; - -/** - * Exception wrapper to associate an exception with a status code at runtime. - * - * @author Rossen Stoyanchev - */ -public class ResponseStatusException extends NestedRuntimeException { - - private final HttpStatus httpStatus; - - - public ResponseStatusException(HttpStatus status) { - this(status, null); - } - - public ResponseStatusException(HttpStatus status, Throwable cause) { - super("Request processing failure with status code: " + status, cause); - this.httpStatus = status; - } - - - public HttpStatus getHttpStatus() { - return this.httpStatus; - } - -} diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapper.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapper.java index 07a39d735f7..c1b27fa7919 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapper.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapper.java @@ -20,7 +20,7 @@ import java.util.function.Function; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.http.HttpStatus; import org.springframework.web.HttpMediaTypeNotAcceptableException; -import org.springframework.web.ResponseStatusException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.bind.annotation.ResponseStatus; /** diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java index add1da9fdcc..6a931a01a1e 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/ResponseStatusExceptionHandler.java @@ -17,7 +17,7 @@ package org.springframework.web.reactive; import reactor.core.publisher.Mono; -import org.springframework.web.ResponseStatusException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.WebExceptionHandler; import org.springframework.web.server.ServerWebExchange; @@ -32,7 +32,7 @@ public class ResponseStatusExceptionHandler implements WebExceptionHandler { @Override public Mono handle(ServerWebExchange exchange, Throwable ex) { if (ex instanceof ResponseStatusException) { - exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getHttpStatus()); + exchange.getResponse().setStatusCode(((ResponseStatusException) ex).getStatus()); return Mono.empty(); } return Mono.error(ex); diff --git a/spring-web-reactive/src/main/java/org/springframework/web/server/ResponseStatusException.java b/spring-web-reactive/src/main/java/org/springframework/web/server/ResponseStatusException.java new file mode 100644 index 00000000000..86cdd15d870 --- /dev/null +++ b/spring-web-reactive/src/main/java/org/springframework/web/server/ResponseStatusException.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.web.server; + +import org.springframework.core.NestedRuntimeException; +import org.springframework.http.HttpStatus; +import org.springframework.util.Assert; + +/** + * Base class for exceptions associated with specific HTTP response status codes. + * + * @author Rossen Stoyanchev + */ +public class ResponseStatusException extends NestedRuntimeException { + + private final HttpStatus status; + + private final String reason; + + + /** + * Constructor with a response code and a reason to add to the exception + * message as explanation. + */ + public ResponseStatusException(HttpStatus status, String reason) { + this(status, reason, null); + } + + /** + * Constructor with a nested exception. + */ + public ResponseStatusException(HttpStatus status, String reason, Throwable cause) { + super("Request failure [status: " + status + ", reason: \"" + reason + "\"]", cause); + Assert.notNull(status, "'status' is required"); + Assert.notNull(reason, "'reason' is required"); + this.status = status; + this.reason = reason; + } + + + /** + * The HTTP status that fits the exception. + */ + public HttpStatus getStatus() { + return this.status; + } + + /** + * The reason explaining the exception. + */ + public String getReason() { + return this.reason; + } + +} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java index 99b99b004b5..cd5dce7650a 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java @@ -42,7 +42,7 @@ import org.springframework.http.server.reactive.MockServerHttpRequest; import org.springframework.http.server.reactive.MockServerHttpResponse; import org.springframework.stereotype.Controller; import org.springframework.web.HttpMediaTypeNotAcceptableException; -import org.springframework.web.ResponseStatusException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapperTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapperTests.java index 4fd4b152b49..8c4ede4ab6d 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapperTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/DispatcherHandlerExceptionMapperTests.java @@ -22,7 +22,7 @@ import org.junit.Test; import org.springframework.http.HttpStatus; import org.springframework.web.HttpMediaTypeNotAcceptableException; -import org.springframework.web.ResponseStatusException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.bind.annotation.ResponseStatus; import static org.junit.Assert.assertEquals; @@ -47,7 +47,7 @@ public class DispatcherHandlerExceptionMapperTests { ex = this.mapper.apply(ex); assertEquals(ResponseStatusException.class, ex.getClass()); - assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getHttpStatus()); + assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getStatus()); } @@ -57,7 +57,7 @@ public class DispatcherHandlerExceptionMapperTests { ex = this.mapper.apply(ex); assertEquals(ResponseStatusException.class, ex.getClass()); - assertEquals(HttpStatus.NOT_ACCEPTABLE, ((ResponseStatusException) ex).getHttpStatus()); + assertEquals(HttpStatus.NOT_ACCEPTABLE, ((ResponseStatusException) ex).getStatus()); } @Test @@ -66,7 +66,7 @@ public class DispatcherHandlerExceptionMapperTests { ex = this.mapper.apply(ex); assertEquals(ResponseStatusException.class, ex.getClass()); - assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus()); + assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getStatus()); } @Test @@ -75,7 +75,7 @@ public class DispatcherHandlerExceptionMapperTests { ex = this.mapper.apply(ex); assertEquals(ResponseStatusException.class, ex.getClass()); - assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus()); + assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getStatus()); } diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java index 37866c5311d..c5b85760a73 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/ResponseStatusExceptionHandlerTests.java @@ -26,7 +26,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.MockServerHttpRequest; import org.springframework.http.server.reactive.MockServerHttpResponse; -import org.springframework.web.ResponseStatusException; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.adapter.DefaultServerWebExchange; import org.springframework.web.server.session.WebSessionManager;