From 9babc4735de2894504e69125174395aaa7349198 Mon Sep 17 00:00:00 2001 From: Luke Morfill Date: Wed, 15 Nov 2023 10:47:39 +0000 Subject: [PATCH] Make WebSocket close exceptions expose the status code given by the server --- .../client/UnexpectedCloseException.java | 26 +++++++++++++++++++ .../GraphQLTransportWSSubprotocolHandler.java | 8 +++--- .../GraphQLWSSubprotocolHandler.java | 8 +++--- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 client/api/src/main/java/io/smallrye/graphql/client/UnexpectedCloseException.java diff --git a/client/api/src/main/java/io/smallrye/graphql/client/UnexpectedCloseException.java b/client/api/src/main/java/io/smallrye/graphql/client/UnexpectedCloseException.java new file mode 100644 index 000000000..dd426af3e --- /dev/null +++ b/client/api/src/main/java/io/smallrye/graphql/client/UnexpectedCloseException.java @@ -0,0 +1,26 @@ +package io.smallrye.graphql.client; + +/** + * Marks a close WebSocket message from the server that was unexpected. + */ +public class UnexpectedCloseException extends InvalidResponseException { + + private final int closeStatusCode; + + public UnexpectedCloseException(String message, int closeStatusCode) { + super(message); + this.closeStatusCode = closeStatusCode; + } + + public UnexpectedCloseException(String message, Throwable cause, int closeStatusCode) { + super(message, cause); + this.closeStatusCode = closeStatusCode; + } + + /** + * The close status code returned by the server. + */ + public int getCloseStatusCode() { + return closeStatusCode; + } +} diff --git a/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqltransportws/GraphQLTransportWSSubprotocolHandler.java b/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqltransportws/GraphQLTransportWSSubprotocolHandler.java index 98f268f5f..28d2ab30a 100644 --- a/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqltransportws/GraphQLTransportWSSubprotocolHandler.java +++ b/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqltransportws/GraphQLTransportWSSubprotocolHandler.java @@ -22,6 +22,7 @@ import io.smallrye.graphql.client.GraphQLClientException; import io.smallrye.graphql.client.GraphQLError; import io.smallrye.graphql.client.InvalidResponseException; +import io.smallrye.graphql.client.UnexpectedCloseException; import io.smallrye.graphql.client.impl.ResponseReader; import io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler; import io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator; @@ -98,12 +99,13 @@ private Uni initialize() { // even if the status code is OK, any unfinished single-result operation // should be marked as failed uniOperations.forEach((id, emitter) -> emitter.fail( - new InvalidResponseException("Connection closed before data was received"))); + new UnexpectedCloseException("Connection closed before data was received", 1000))); multiOperations.forEach((id, emitter) -> emitter.complete()); } else { - InvalidResponseException exception = new InvalidResponseException( + UnexpectedCloseException exception = new UnexpectedCloseException( "Server closed the websocket connection with code: " - + webSocket.closeStatusCode() + " and reason: " + webSocket.closeReason()); + + webSocket.closeStatusCode() + " and reason: " + webSocket.closeReason(), + webSocket.closeStatusCode()); uniOperations.forEach((id, emitter) -> emitter.fail(exception)); multiOperations.forEach((id, emitter) -> emitter.fail(exception)); } diff --git a/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqlws/GraphQLWSSubprotocolHandler.java b/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqlws/GraphQLWSSubprotocolHandler.java index 2588565b3..60945ee69 100644 --- a/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqlws/GraphQLWSSubprotocolHandler.java +++ b/client/implementation-vertx/src/main/java/io/smallrye/graphql/client/vertx/websocket/graphqlws/GraphQLWSSubprotocolHandler.java @@ -19,6 +19,7 @@ import io.smallrye.graphql.client.GraphQLClientException; import io.smallrye.graphql.client.GraphQLError; import io.smallrye.graphql.client.InvalidResponseException; +import io.smallrye.graphql.client.UnexpectedCloseException; import io.smallrye.graphql.client.impl.ResponseReader; import io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler; import io.smallrye.graphql.client.vertx.websocket.opid.IncrementingNumberOperationIDGenerator; @@ -79,12 +80,13 @@ private Uni initialize() { // even if the status code is OK, any unfinished single-result operation // should be marked as failed uniOperations.forEach((id, emitter) -> emitter.fail( - new InvalidResponseException("Connection closed before data was received"))); + new UnexpectedCloseException("Connection closed before data was received", 1000))); multiOperations.forEach((id, emitter) -> emitter.complete()); } else { - InvalidResponseException exception = new InvalidResponseException( + UnexpectedCloseException exception = new UnexpectedCloseException( "Server closed the websocket connection with code: " - + webSocket.closeStatusCode() + " and reason: " + webSocket.closeReason()); + + webSocket.closeStatusCode() + " and reason: " + webSocket.closeReason(), + webSocket.closeStatusCode()); uniOperations.forEach((id, emitter) -> emitter.fail(exception)); multiOperations.forEach((id, emitter) -> emitter.fail(exception)); }