Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

NC-1950: Fixing WebSocket error response #292

Merged
merged 2 commits into from
Nov 21, 2018
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
Expand Up @@ -14,6 +14,7 @@

import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketRpcRequest;

import java.util.Map;
Expand Down Expand Up @@ -45,12 +46,13 @@ public void handle(final String id, final Buffer buffer) {
request = buffer.toJsonObject().mapTo(WebSocketRpcRequest.class);
} catch (final IllegalArgumentException | DecodeException e) {
LOG.debug("Error mapping json to WebSocketRpcRequest", e);
future.complete(JsonRpcError.INVALID_REQUEST);
future.complete(new JsonRpcErrorResponse(null, JsonRpcError.INVALID_REQUEST));
return;
}

if (!methods.containsKey(request.getMethod())) {
future.complete(JsonRpcError.METHOD_NOT_FOUND);
future.complete(
new JsonRpcErrorResponse(request.getId(), JsonRpcError.METHOD_NOT_FOUND));
LOG.debug("Can't find method {}", request.getMethod());
return;
}
Expand All @@ -61,14 +63,16 @@ public void handle(final String id, final Buffer buffer) {
future.complete(method.response(request));
} catch (final Exception e) {
LOG.error(JsonRpcError.INTERNAL_ERROR.getMessage(), e);
future.complete(JsonRpcError.INTERNAL_ERROR);
future.complete(new JsonRpcErrorResponse(request.getId(), JsonRpcError.INTERNAL_ERROR));
}
},
result -> {
if (result.succeeded()) {
replyToClient(id, Json.encodeToBuffer(result.result()));
} else {
replyToClient(id, Json.encodeToBuffer(JsonRpcError.INTERNAL_ERROR));
replyToClient(
id,
Json.encodeToBuffer(new JsonRpcErrorResponse(null, JsonRpcError.INTERNAL_ERROR)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketRpcRequest;

Expand Down Expand Up @@ -95,14 +96,17 @@ public void handlerDeliversResponseSuccessfully(final TestContext context) {
public void jsonDecodeFailureShouldRespondInvalidRequest(final TestContext context) {
final Async async = context.async();

final JsonRpcErrorResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.INVALID_REQUEST);

final String websocketId = UUID.randomUUID().toString();

vertx
.eventBus()
.consumer(websocketId)
.handler(
msg -> {
context.assertEquals(Json.encode(JsonRpcError.INVALID_REQUEST), msg.body());
context.assertEquals(Json.encode(expectedResponse), msg.body());
verifyZeroInteractions(jsonRpcMethodMock);
async.complete();
})
Expand All @@ -115,14 +119,17 @@ public void jsonDecodeFailureShouldRespondInvalidRequest(final TestContext conte
public void objectMapperFailureShouldRespondInvalidRequest(final TestContext context) {
final Async async = context.async();

final JsonRpcErrorResponse expectedResponse =
new JsonRpcErrorResponse(null, JsonRpcError.INVALID_REQUEST);

final String websocketId = UUID.randomUUID().toString();

vertx
.eventBus()
.consumer(websocketId)
.handler(
msg -> {
context.assertEquals(Json.encode(JsonRpcError.INVALID_REQUEST), msg.body());
context.assertEquals(Json.encode(expectedResponse), msg.body());
verifyZeroInteractions(jsonRpcMethodMock);
async.complete();
})
Expand All @@ -137,6 +144,8 @@ public void absentMethodShouldRespondMethodNotFound(final TestContext context) {

final JsonObject requestJson =
new JsonObject().put("id", 1).put("method", "eth_nonexistentMethod");
final JsonRpcErrorResponse expectedResponse =
new JsonRpcErrorResponse(1, JsonRpcError.METHOD_NOT_FOUND);

final String websocketId = UUID.randomUUID().toString();

Expand All @@ -145,7 +154,7 @@ public void absentMethodShouldRespondMethodNotFound(final TestContext context) {
.consumer(websocketId)
.handler(
msg -> {
context.assertEquals(Json.encode(JsonRpcError.METHOD_NOT_FOUND), msg.body());
context.assertEquals(Json.encode(expectedResponse), msg.body());
async.complete();
})
.completionHandler(v -> handler.handle(websocketId, Buffer.buffer(requestJson.toString())));
Expand All @@ -160,6 +169,8 @@ public void onExceptionProcessingRequestShouldRespondInternalError(final TestCon
final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
final JsonRpcRequest expectedRequest = requestJson.mapTo(WebSocketRpcRequest.class);
when(jsonRpcMethodMock.response(eq(expectedRequest))).thenThrow(new RuntimeException());
final JsonRpcErrorResponse expectedResponse =
new JsonRpcErrorResponse(1, JsonRpcError.INTERNAL_ERROR);

final String websocketId = UUID.randomUUID().toString();

Expand All @@ -168,7 +179,7 @@ public void onExceptionProcessingRequestShouldRespondInternalError(final TestCon
.consumer(websocketId)
.handler(
msg -> {
context.assertEquals(Json.encode(JsonRpcError.INTERNAL_ERROR), msg.body());
context.assertEquals(Json.encode(expectedResponse), msg.body());
async.complete();
})
.completionHandler(v -> handler.handle(websocketId, Buffer.buffer(requestJson.toString())));
Expand Down