diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java index eb0006a8f..c6f3ce5ab 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java @@ -128,21 +128,17 @@ private String getExchangeDesc(ServerWebExchange exchange) { } protected Mono lookupRoute(ServerWebExchange exchange) { - return this.routeLocator.getRoutes() - // individually filter routes so that filterWhen error delaying is not a - // problem - .concatMap(route -> Mono.just(route).filterWhen(r -> { - // add the current route we are testing - exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId()); - return r.getPredicate().apply(exchange); - }) - // instead of immediately stopping main flux due to error, log and - // swallow it - .doOnError(e -> logger.error("Error applying predicate for route: " + route.getId(), e)) - .onErrorResume(e -> Mono.empty())) - // .defaultIfEmpty() put a static Route not found - // or .switchIfEmpty() - // .switchIfEmpty(Mono.empty().log("noroute")) + return this.routeLocator.getRoutes().filterWhen(route -> { + // add the current route we are testing + exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId()); + try { + return route.getPredicate().apply(exchange); + } + catch (Exception e) { + logger.error("Error applying predicate for route: " + route.getId(), e); + } + return Mono.just(false); + }) .next() // TODO: error handling .map(route -> { diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java index cee04bdbb..b97a3bc61 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java @@ -71,7 +71,7 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) { Route routeError = Route.async() .id("routeError") .uri("http://localhost") - .asyncPredicate(swe -> Mono.error(new IllegalStateException("boom1"))) + .asyncPredicate(swe -> Mono.just(boom1())) .build(); Route routeFail = Route.async().id("routeFail").uri("http://localhost").asyncPredicate(swe -> { throw new IllegalStateException("boom2"); @@ -96,4 +96,8 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) { Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2")); } + boolean boom1() { + throw new IllegalStateException("boom1"); + } + }