Browse Source

Replace constant exceptions with inlined ones

Issue: SRP-17475
pull/2017/head
Rossen Stoyanchev 6 years ago
parent
commit
9f857c1f16
  1. 11
      spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java
  2. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java
  3. 17
      spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java
  4. 11
      spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java

11
spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java

@ -142,16 +142,23 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -142,16 +142,23 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
@Override
public Mono<Void> handle(ServerWebExchange exchange) {
if (this.handlerMappings == null) {
return Mono.error(HANDLER_NOT_FOUND_EXCEPTION);
return createNotFoundError();
}
return Flux.fromIterable(this.handlerMappings)
.concatMap(mapping -> mapping.getHandler(exchange))
.next()
.switchIfEmpty(Mono.error(HANDLER_NOT_FOUND_EXCEPTION))
.switchIfEmpty(createNotFoundError())
.flatMap(handler -> invokeHandler(exchange, handler))
.flatMap(result -> handleResult(exchange, result));
}
private <R> Mono<R> createNotFoundError() {
return Mono.defer(() -> {
Exception ex = new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler");
return Mono.error(ex);
});
}
private Mono<HandlerResult> invokeHandler(ServerWebExchange exchange, Object handler) {
if (this.handlerAdapters != null) {
for (HandlerAdapter handlerAdapter : this.handlerAdapters) {

4
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

@ -88,8 +88,6 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { @@ -88,8 +88,6 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
private static final Set<HttpMethod> SUPPORTED_METHODS = EnumSet.of(HttpMethod.GET, HttpMethod.HEAD);
private static final Exception NOT_FOUND_EXCEPTION = new ResponseStatusException(HttpStatus.NOT_FOUND);
private static final Log logger = LogFactory.getLog(ResourceWebHandler.class);
@ -324,7 +322,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { @@ -324,7 +322,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
return getResource(exchange)
.switchIfEmpty(Mono.defer(() -> {
logger.debug(exchange.getLogPrefix() + "Resource not found");
return Mono.error(NOT_FOUND_EXCEPTION);
return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND));
}))
.flatMap(resource -> {
try {

17
spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java

@ -19,6 +19,7 @@ package org.springframework.web.reactive; @@ -19,6 +19,7 @@ package org.springframework.web.reactive;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
import org.junit.Test;
@ -81,15 +82,19 @@ public class DispatcherHandlerErrorTests { @@ -81,15 +82,19 @@ public class DispatcherHandlerErrorTests {
@Test
public void noHandler() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/does-not-exist"));
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
Mono<Void> mono = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher)
.consumeErrorWith(error -> {
assertThat(error, instanceOf(ResponseStatusException.class));
assertThat(error.getMessage(),
is("404 NOT_FOUND \"No matching handler\""));
StepVerifier.create(mono)
.consumeErrorWith(ex -> {
assertThat(ex, instanceOf(ResponseStatusException.class));
assertThat(ex.getMessage(), is("404 NOT_FOUND \"No matching handler\""));
})
.verify();
// SPR-17475
AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
StepVerifier.create(mono).consumeErrorWith(exceptionRef::set).verify();
StepVerifier.create(mono).consumeErrorWith(ex -> assertNotSame(exceptionRef.get(), ex)).verify();
}
@Test

11
spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java

@ -24,6 +24,7 @@ import java.util.Arrays; @@ -24,6 +24,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
import org.junit.Test;
@ -274,6 +275,7 @@ public class ResourceWebHandlerTests { @@ -274,6 +275,7 @@ public class ResourceWebHandlerTests {
private void testInvalidPath(String requestPath, ResourceWebHandler handler) {
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(""));
setPathWithinHandlerMapping(exchange, requestPath);
StepVerifier.create(handler.handle(exchange))
.expectErrorSatisfies(err -> {
assertThat(err, instanceOf(ResponseStatusException.class));
@ -468,11 +470,18 @@ public class ResourceWebHandlerTests { @@ -468,11 +470,18 @@ public class ResourceWebHandlerTests {
MockServerHttpRequest request = MockServerHttpRequest.method(httpMethod, "").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
setPathWithinHandlerMapping(exchange, "not-there.css");
StepVerifier.create(this.handler.handle(exchange))
Mono<Void> mono = this.handler.handle(exchange);
StepVerifier.create(mono)
.expectErrorSatisfies(err -> {
assertThat(err, instanceOf(ResponseStatusException.class));
assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus());
}).verify(TIMEOUT);
// SPR-17475
AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
StepVerifier.create(mono).consumeErrorWith(exceptionRef::set).verify();
StepVerifier.create(mono).consumeErrorWith(ex -> assertNotSame(exceptionRef.get(), ex)).verify();
}
@Test

Loading…
Cancel
Save