Skip to content

Commit

Permalink
Correctly forward websocket errors in DevUIJsonRPCTest
Browse files Browse the repository at this point in the history
Instead of logging them (sometimes) and waiting for the timeout.
  • Loading branch information
yrodiere committed Mar 1, 2024
1 parent 24cd4b9 commit e386b14
Showing 1 changed file with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ private JsonNode objectResultFromJsonRPC(int id) throws InterruptedException, Js
}

private JsonNode objectResultFromJsonRPC(int id, int loopCount) throws InterruptedException, JsonProcessingException {
if (MESSAGES.containsKey(id)) {
String response = MESSAGES.remove(id);
if (RESPONSES.containsKey(id)) {
WebSocketResponse response = RESPONSES.remove(id);
if (response != null) {
ObjectNode json = (ObjectNode) new ObjectMapper().readTree(response);
ObjectNode json = (ObjectNode) new ObjectMapper().readTree(response.message());
JsonNode result = json.get("result");
if (result != null) {
return result.get("object");
Expand Down Expand Up @@ -212,25 +212,48 @@ private int sendRequest(String methodName, Map<String, Object> params) throws IO
socket.frameHandler((e) -> {
Buffer b = accumulatedBuffer.appendBuffer(e.binaryData());
if (e.isFinal()) {
MESSAGES.put(id, b.toString());
RESPONSES.put(id, new WebSocketResponse(b.toString()));
}
});

socket.writeTextMessage(request);

socket.exceptionHandler((e) -> {
e.printStackTrace();
RESPONSES.put(id, new WebSocketResponse(e));
vertx.close();
});
socket.closeHandler(v -> {
vertx.close();
});
} else {
RESPONSES.put(id, new WebSocketResponse(ar.cause()));
vertx.close();
}
});
return id;
}

private static final ConcurrentHashMap<Integer, String> MESSAGES = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Integer, WebSocketResponse> RESPONSES = new ConcurrentHashMap<>();

private static class WebSocketResponse {
private final String message;
private final Throwable throwable;

public WebSocketResponse(String message) {
this.message = message;
this.throwable = null;
}

public WebSocketResponse(Throwable throwable) {
this.message = null;
this.throwable = throwable;
}

String message() {
if (throwable != null) {
throw new IllegalStateException("Request failed: " + throwable.getMessage(), throwable);
}
return message;
}
}
}

0 comments on commit e386b14

Please sign in to comment.