Skip to content

Commit

Permalink
Make WebSocket close exceptions expose the status code given by the s…
Browse files Browse the repository at this point in the history
…erver
  • Loading branch information
rubik-cube-man authored and jmartisk committed Nov 15, 2023
1 parent 9f988f7 commit 7d87fcf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,12 +99,13 @@ private Uni<Void> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,12 +80,13 @@ private Uni<Void> 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));
}
Expand Down

0 comments on commit 7d87fcf

Please sign in to comment.