From f61dfc56fec666860323dde933ced7afac296c36 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Thu, 25 Apr 2024 12:33:56 +0100 Subject: [PATCH] Align future handling with AsyncServerResponse See gh-959 --- .../server/webmvc/GraphQlHttpHandler.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java index 71536fd5..7568cd9c 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java @@ -21,7 +21,6 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -import jakarta.servlet.ServletException; import reactor.core.publisher.Mono; import org.springframework.graphql.server.WebGraphQlHandler; @@ -70,8 +69,7 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler { @Override - protected ServerResponse prepareResponse(ServerRequest request, Mono responseMono) - throws ServletException { + protected ServerResponse prepareResponse(ServerRequest request, Mono responseMono) { CompletableFuture future = responseMono.map((response) -> { MediaType contentType = selectResponseMediaType(request); @@ -87,15 +85,15 @@ public class GraphQlHttpHandler extends AbstractGraphQlHttpHandler { } }).toFuture(); - if (future.isDone()) { + // This won't be needed with a Spring Framework 6.2 baseline: + // https://github.com/spring-projects/spring-framework/issues/32223 + + if (future.isDone() && !future.isCancelled() && !future.isCompletedExceptionally()) { try { return future.get(); } - catch (ExecutionException ex) { - throw new ServletException(ex.getCause()); - } - catch (InterruptedException ex) { - throw new ServletException(ex); + catch (InterruptedException | ExecutionException ignored) { + // fall through to use DefaultAsyncServerResponse } }