Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose close status codes for WebSockets #1968

Merged
merged 1 commit into from
Nov 15, 2023
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
@@ -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
Loading