Skip to content

Commit 2287afe

Browse files
author
tassoc
committed
fix error handling for async predicate
Signed-off-by: tassoc <tassoc76@gmail.com>
1 parent 86b4529 commit 2287afe

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,18 @@ protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
131131
return this.routeLocator.getRoutes().filterWhen(route -> {
132132
// add the current route we are testing
133133
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId());
134-
try {
135-
return route.getPredicate().apply(exchange);
136-
}
137-
catch (Exception e) {
134+
135+
return Mono.defer(() -> Mono.from(route.getPredicate().apply(exchange))).onErrorResume(e -> {
138136
logger.error("Error applying predicate for route: " + route.getId(), e);
139-
}
140-
return Mono.just(false);
141-
})
142-
.next()
143-
// TODO: error handling
144-
.map(route -> {
145-
if (logger.isDebugEnabled()) {
146-
logger.debug("Route matched: " + route.getId());
147-
}
148-
validateRoute(route, exchange);
149-
return route;
137+
return Mono.just(false);
150138
});
151-
139+
}).next().map(route -> {
140+
if (logger.isDebugEnabled()) {
141+
logger.debug("Route matched: " + route.getId());
142+
}
143+
validateRoute(route, exchange);
144+
return route;
145+
});
152146
/*
153147
* TODO: trace logging if (logger.isTraceEnabled()) {
154148
* logger.trace("RouteDefinition did not match: " + routeDefinition.getId()); }

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
7474
.uri("http://localhost")
7575
.asyncPredicate(swe -> Mono.just(boom1()))
7676
.build();
77+
Route routeMonoError = Route.async()
78+
.id("routeMonoError")
79+
.uri("http://localhost")
80+
.asyncPredicate(swe -> Mono.error(new IllegalStateException("boom3")))
81+
.build();
7782
Route routeFail = Route.async().id("routeFail").uri("http://localhost").asyncPredicate(swe -> {
7883
throw new IllegalStateException("boom2");
7984
}).build();
@@ -82,7 +87,8 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
8287
.uri("http://localhost")
8388
.asyncPredicate(swe -> Mono.just(true))
8489
.build();
85-
RouteLocator routeLocator = () -> Flux.just(routeFalse, routeError, routeFail, routeTrue).hide();
90+
RouteLocator routeLocator = () -> Flux.just(routeFalse, routeError, routeMonoError, routeFail, routeTrue)
91+
.hide();
8692
RoutePredicateHandlerMapping mapping = new RoutePredicateHandlerMapping(null, routeLocator,
8793
new GlobalCorsProperties(), new MockEnvironment());
8894

@@ -95,6 +101,9 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
95101

96102
assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")).isTrue();
97103
assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2")).isTrue();
104+
105+
assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeMonoError")).isTrue();
106+
assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom3")).isTrue();
98107
}
99108

100109
boolean boom1() {

0 commit comments

Comments
 (0)