Skip to content
Merged
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 @@ -50,6 +50,9 @@

import static org.springframework.cloud.gateway.server.mvc.common.MvcUtils.getApplicationContext;

/**
* @author Samuel Schnegg
*/
public abstract class LoadBalancerFilterFunctions {

private static final Log log = LogFactory.getLog(LoadBalancerFilterFunctions.class);
Expand Down Expand Up @@ -122,7 +125,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> lb(String se
supportedLifecycleProcessors.forEach(lifecycle -> lifecycle.onComplete(
new CompletionContext<>(CompletionContext.Status.FAILED, e, lbRequest, defaultResponse)));

throw new RuntimeException(e);
throw e;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.function.HandlerFilterFunction;
import org.springframework.web.servlet.function.HandlerFunction;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.ServerRequest;
Expand Down Expand Up @@ -369,6 +370,19 @@ public void loadbalancerWorks() {
});
}

@Test
public void loadbalancerDoesNotWrapException() {
restClient.get()
.uri("/anything/loadbalancer-does-not-wrap-exception")
.exchange()
.expectStatus()
.is5xxServerError()
.expectHeader()
.valueEquals("X-Exception-ClassName", "IOException")
.expectHeader()
.valueEquals("X-Exception-Message", "A custom exception");
}

@Test
public void hostPredicateWorks() {
String host = "www1.myjavadslhost.com";
Expand Down Expand Up @@ -1188,6 +1202,37 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsLoadBalancer() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsLoadBalancerDoesNotWrapException() {
HandlerFilterFunction<ServerResponse, ServerResponse> addExceptionDetailsInResponseHeadersFilter = (request,
next) -> {
try {
return next.handle(request);
}
catch (Exception e) {
return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
.header("X-Exception-ClassName", e.getClass().getSimpleName())
.header("X-Exception-Message", e.getMessage())
.build();
}

};

var throwingExceptionFilter = (HandlerFilterFunction<ServerResponse, ServerResponse>) (request, next) -> {
throw new IOException("A custom exception");
};

// @formatter:off
return route()
.GET("/anything/loadbalancer-does-not-wrap-exception", http())
.filter(addExceptionDetailsInResponseHeadersFilter)
.filter(lb("httpbin"))
.filter(throwingExceptionFilter)
.withAttribute(MvcUtils.GATEWAY_ROUTE_ID_ATTR, "testloadbalancerForException")
.build();
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsHost() {
// @formatter:off
Expand Down