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 committed Nov 15, 2023
1 parent 9f988f7 commit 77ff28d
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 @@ -9,6 +9,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import io.smallrye.graphql.client.UnexpectedCloseException;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
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 @@ -7,6 +7,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

import io.smallrye.graphql.client.UnexpectedCloseException;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
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 77ff28d

Please sign in to comment.