use of io.smallrye.graphql.client.InvalidResponseException in project smallrye-graphql by smallrye.
the class GraphQLTransportWSSubprotocolHandler method handleComplete.
private void handleComplete(String operationId) {
UniEmitter<? super String> emitter = uniOperations.remove(operationId);
if (emitter != null) {
// For a uni operation, we should have received a 'next' message before the 'complete' message.
// If that happened, the emitter was already completed and operation removed from the map.
// If that didn't happen, then this is an issue with the server, let's fail the operation then.
emitter.fail(new InvalidResponseException("Protocol error: received a 'complete' message for" + " this operation before the actual data"));
} else {
MultiEmitter<? super String> multiEmitter = multiOperations.remove(operationId);
if (multiEmitter != null) {
log.debug("Completed operation " + operationId);
multiEmitter.complete();
}
}
}
use of io.smallrye.graphql.client.InvalidResponseException in project smallrye-graphql by smallrye.
the class GraphQLWSSubprotocolHandler method initialize.
private Uni<Void> initialize() {
return Uni.createFrom().emitter(initializationEmitter -> {
if (log.isTraceEnabled()) {
log.trace("Initializing websocket with graphql-ws protocol");
}
webSocket.closeHandler((v) -> {
onClose.run();
if (webSocket.closeStatusCode() != null) {
if (webSocket.closeStatusCode() == 1000) {
log.debug("WebSocket closed with status code 1000");
// 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")));
multiOperations.forEach((id, emitter) -> emitter.complete());
} else {
InvalidResponseException exception = new InvalidResponseException("Server closed the websocket connection with code: " + webSocket.closeStatusCode() + " and reason: " + webSocket.closeReason());
uniOperations.forEach((id, emitter) -> emitter.fail(exception));
multiOperations.forEach((id, emitter) -> emitter.fail(exception));
}
} else {
InvalidResponseException exception = new InvalidResponseException("Connection closed");
uniOperations.forEach((id, emitter) -> emitter.fail(exception));
multiOperations.forEach((id, emitter) -> emitter.fail(exception));
}
});
webSocket.exceptionHandler(this::failAllActiveOperationsWith);
send(webSocket, createConnectionInitMessage());
// set up a timeout for subscription initialization
Cancellable timeoutWaitingForConnectionAckMessage = null;
if (subscriptionInitializationTimeout != null) {
timeoutWaitingForConnectionAckMessage = Uni.createFrom().item(1).onItem().delayIt().by(Duration.ofMillis(subscriptionInitializationTimeout)).subscribe().with(timeout -> {
initializationEmitter.fail(new InvalidResponseException("Server did not send a connection_ack message"));
webSocket.close((short) 1002, "Timeout waiting for a connection_ack message");
});
}
// make an effectively final copy of this value to use it in a lambda expression
Cancellable finalTimeoutWaitingForConnectionAckMessage = timeoutWaitingForConnectionAckMessage;
webSocket.handler(text -> {
if (log.isTraceEnabled()) {
log.trace("<<< " + text);
}
try {
JsonObject message = parseIncomingMessage(text.toString());
MessageType messageType = getMessageType(message);
switch(messageType) {
case GQL_CONNECTION_ERROR:
failAllActiveOperationsWith(new InvalidResponseException(message.get("payload").toString()));
webSocket.close();
break;
case GQL_CONNECTION_ACK:
if (finalTimeoutWaitingForConnectionAckMessage != null) {
finalTimeoutWaitingForConnectionAckMessage.cancel();
}
initializationEmitter.complete(null);
break;
case GQL_DATA:
handleData(message.getString("id"), message.getJsonObject("payload"));
break;
case GQL_ERROR:
handleOperationError(message.getString("id"), message.getJsonObject("payload"));
break;
case GQL_COMPLETE:
handleComplete(message.getString("id"));
break;
case GQL_START:
case GQL_STOP:
case GQL_CONNECTION_KEEP_ALIVE:
case GQL_CONNECTION_INIT:
case GQL_CONNECTION_TERMINATE:
break;
}
} catch (JsonParsingException | IllegalArgumentException e) {
log.error("Unexpected message from server: " + text);
// should we fail the operations here?
}
});
});
}
use of io.smallrye.graphql.client.InvalidResponseException in project smallrye-graphql by smallrye.
the class JsonArrayReader method readItem.
private Object readItem(IndexedLocationBuilder locationBuilder, JsonValue itemValue) {
Location itemLocation = locationBuilder.nextLocation();
TypeInfo itemType = getItemType();
if (itemValue.getValueType() == ValueType.NULL && itemType.isNonNull())
throw new InvalidResponseException("invalid null " + itemLocation);
return JsonReader.readJson(itemLocation, itemType, itemValue, field);
}
use of io.smallrye.graphql.client.InvalidResponseException in project smallrye-graphql by smallrye.
the class JsonMapReader method read.
@Override
Object read() {
GraphQLClientValueHelper.check(location, value, type.isMap());
MapLocationBuilder locationBuilder = new MapLocationBuilder(location);
Map result = new HashMap<>();
for (JsonValue entry : value) {
Location keyLocation = locationBuilder.nextKeyLocation();
Location valueLocation = locationBuilder.nextValueLocation();
JsonValue keyJson = entry.asJsonObject().get(JSON_KEY_FOR_KEY);
if (keyJson.getValueType() == JsonValue.ValueType.NULL) {
throw new InvalidResponseException("unexpected null key at " + keyLocation);
}
JsonValue valueJson = entry.asJsonObject().get(JSON_KEY_FOR_VALUE);
if (valueJson.getValueType() == JsonValue.ValueType.NULL && valueType.isNonNull()) {
throw new InvalidResponseException("unexpected null value at " + keyLocation);
}
Object keyDeserialized = JsonReader.readJson(keyLocation, keyType, keyJson, field);
Object valueDeserialized = JsonReader.readJson(valueLocation, valueType, valueJson, field);
result.put(keyDeserialized, valueDeserialized);
}
return result;
}
use of io.smallrye.graphql.client.InvalidResponseException in project smallrye-graphql by smallrye.
the class JsonObjectReader method buildValue.
private Object buildValue(Location location, JsonObject value, FieldInfo field) {
String fieldName = field.getAlias().orElseGet(field::getName);
Location fieldLocation = new Location(field.getType(), location.getDescription() + "." + fieldName);
JsonValue jsonFieldValue = value.get(fieldName);
if (jsonFieldValue == null) {
if (field.isNonNull())
throw new InvalidResponseException("missing " + fieldLocation);
return null;
}
return readJson(fieldLocation, field.getType(), jsonFieldValue, field);
}
Aggregations