Skip to content

fix error handling for async predicate #3811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,18 @@ protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
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) {

return Mono.defer(() -> Mono.from(route.getPredicate().apply(exchange))).onErrorResume(e -> {
logger.error("Error applying predicate for route: " + route.getId(), e);
}
return Mono.just(false);
})
.next()
// TODO: error handling
.map(route -> {
if (logger.isDebugEnabled()) {
logger.debug("Route matched: " + route.getId());
}
validateRoute(route, exchange);
return route;
return Mono.just(false);
});

}).next().map(route -> {
if (logger.isDebugEnabled()) {
logger.debug("Route matched: " + route.getId());
}
validateRoute(route, exchange);
return route;
});
/*
* TODO: trace logging if (logger.isTraceEnabled()) {
* logger.trace("RouteDefinition did not match: " + routeDefinition.getId()); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
.uri("http://localhost")
.asyncPredicate(swe -> Mono.just(boom1()))
.build();
Route routeMonoError = Route.async()
.id("routeMonoError")
.uri("http://localhost")
.asyncPredicate(swe -> Mono.error(new IllegalStateException("boom3")))
.build();
Route routeFail = Route.async().id("routeFail").uri("http://localhost").asyncPredicate(swe -> {
throw new IllegalStateException("boom2");
}).build();
Expand All @@ -82,7 +87,8 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
.uri("http://localhost")
.asyncPredicate(swe -> Mono.just(true))
.build();
RouteLocator routeLocator = () -> Flux.just(routeFalse, routeError, routeFail, routeTrue).hide();
RouteLocator routeLocator = () -> Flux.just(routeFalse, routeError, routeMonoError, routeFail, routeTrue)
.hide();
RoutePredicateHandlerMapping mapping = new RoutePredicateHandlerMapping(null, routeLocator,
new GlobalCorsProperties(), new MockEnvironment());

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

assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeFail")).isTrue();
assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2")).isTrue();

assertThat(capturedOutput.getOut().contains("Error applying predicate for route: routeMonoError")).isTrue();
assertThat(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom3")).isTrue();
}

boolean boom1() {
Expand Down