Skip to content

Commit

Permalink
Merge pull request #267 from guilhermeblanco/log-errors-properly
Browse files Browse the repository at this point in the history
Added error logging and error messaging response when errors occur
  • Loading branch information
oliemansm authored Dec 18, 2020
2 parents 9fe1fe9 + ef9d8f9 commit c5609b7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void sendDataMessage(String id, Object payload) {
}

@Override
public void sendErrorMessage(String id) {

public void sendErrorMessage(String id, Object payload) {
send(mapper.serialize(payload));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package graphql.kickstart.execution.subscriptions;

import graphql.ExecutionResult;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import graphql.GraphQLError;
import graphql.kickstart.execution.error.GenericGraphQLError;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Subscriber;
Expand All @@ -29,15 +34,20 @@ public void onSubscribe(Subscription subscription) {
public void onNext(ExecutionResult executionResult) {
Map<String, Object> result = new HashMap<>();
result.put("data", executionResult.getData());

session.sendDataMessage(id, result);
subscriptionReference.get().request(1);
}

@Override
public void onError(Throwable throwable) {
log.error("Subscription error", throwable);

Map<String, Object> payload = new HashMap<>();
payload.put("errors", Collections.singletonList(new GenericGraphQLError(throwable.getMessage())));

session.unsubscribe(id);
session.sendErrorMessage(id);
session.sendErrorMessage(id, payload);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface SubscriptionSession {

void sendDataMessage(String id, Object payload);

void sendErrorMessage(String id);
void sendErrorMessage(String id, Object payload);

void sendCompleteMessage(String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void sendDataMessage(String id, Object payload) {
}

@Override
public void sendErrorMessage(String id) {
sendMessage(new OperationMessage(Type.GQL_ERROR, id, null));
public void sendErrorMessage(String id, Object payload) {
sendMessage(new OperationMessage(Type.GQL_ERROR, id, payload));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void apply(SubscriptionSession session, OperationMessage message) {
connectionListeners.forEach(it -> it.onConnect(session, message));
session.sendMessage(new OperationMessage(Type.GQL_CONNECTION_ACK, message.getId(), null));
} catch (Throwable t) {
session.sendMessage(
new OperationMessage(Type.GQL_CONNECTION_ERROR, message.getId(), t.getMessage()));
log.error("Cannot initialize subscription command '{}'", message, t);
session.sendMessage(new OperationMessage(Type.GQL_CONNECTION_ERROR, message.getId(), t.getMessage()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onNext(String message) {

@Override
public void onError(Throwable t) {

log.error("WebSocket error", t);
}

@Override
Expand Down

0 comments on commit c5609b7

Please sign in to comment.