Improve PluginRpcEndpointException log (#6789)

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
pull/6798/head
Fabio Di Fabio 8 months ago committed by GitHub
parent c9c237e57d
commit 7abb29cdc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/PluginJsonRpcMethod.java
  2. 20
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestHelper.java
  3. 45
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/jsonrpc/PluginJsonRpcMethodTest.java
  4. 2
      plugin-api/build.gradle
  5. 32
      plugin-api/src/main/java/org/hyperledger/besu/plugin/services/exception/PluginRpcEndpointException.java

@ -50,8 +50,8 @@ public class PluginJsonRpcMethod implements JsonRpcMethod {
final Object result = function.apply(() -> request.getRequest().getParams());
return new JsonRpcSuccessResponse(request.getRequest().getId(), result);
} catch (final PluginRpcEndpointException ex) {
final JsonRpcError error = new JsonRpcError(ex.getRpcMethodError(), ex.getMessage());
LOG.error("Error calling plugin JSON-RPC endpoint", ex);
final JsonRpcError error = new JsonRpcError(ex.getRpcMethodError(), ex.getData());
LOG.debug("Error calling plugin JSON-RPC endpoint", ex);
return new JsonRpcErrorResponse(request.getRequest().getId(), error);
}
}

@ -37,8 +37,16 @@ public class JsonRpcTestHelper {
}
protected void assertValidJsonRpcError(
final JsonObject json, final Object id, final int errorCode, final String errorMessage)
throws Exception {
final JsonObject json, final Object id, final int errorCode, final String errorMessage) {
assertValidJsonRpcError(json, id, errorCode, errorMessage, null);
}
protected void assertValidJsonRpcError(
final JsonObject json,
final Object id,
final int errorCode,
final String errorMessage,
final String data) {
// Check all expected fieldnames are set
final Set<String> fieldNames = json.fieldNames();
assertThat(fieldNames.size()).isEqualTo(3);
@ -53,13 +61,19 @@ public class JsonRpcTestHelper {
// Check error format
final JsonObject error = json.getJsonObject("error");
final Set<String> errorFieldNames = error.fieldNames();
assertThat(errorFieldNames.size()).isEqualTo(2);
assertThat(errorFieldNames.size()).isEqualTo(data == null ? 2 : 3);
assertThat(errorFieldNames.contains("code")).isTrue();
assertThat(errorFieldNames.contains("message")).isTrue();
if (data != null) {
assertThat(errorFieldNames.contains("data")).isTrue();
}
// Check error field values
assertThat(error.getInteger("code")).isEqualTo(errorCode);
assertThat(error.getString("message")).isEqualTo(errorMessage);
if (data != null) {
assertThat(error.getString("data")).isEqualTo(data);
}
}
protected void assertIdMatches(final JsonObject json, final Object expectedId) {

@ -24,6 +24,9 @@ import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException
import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest;
import org.hyperledger.besu.plugin.services.rpc.RpcMethodError;
import java.util.Locale;
import java.util.Optional;
import io.vertx.core.json.JsonObject;
import okhttp3.RequestBody;
import okhttp3.Response;
@ -127,6 +130,25 @@ public class PluginJsonRpcMethodTest extends JsonRpcHttpServiceTestBase {
}
}
@Test
public void methodErrorWithDataShouldReturnErrorResponseWithDecodedData() throws Exception {
final var wrongParamContent =
"""
{"jsonrpc":"2.0","id":1,"method":"plugin_echo","params":["data"]}""";
try (var unused =
addRpcMethod(
"plugin_echo",
new PluginJsonRpcMethod("plugin_echo", PluginJsonRpcMethodTest::echoPluginRpcMethod))) {
final RequestBody body = RequestBody.create(wrongParamContent, JSON);
try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
assertThat(resp.code()).isEqualTo(200);
final JsonObject json = new JsonObject(resp.body().string());
testHelper.assertValidJsonRpcError(json, 1, -2, "Error with data: ABC", "abc");
}
}
}
@Test
public void unhandledExceptionShouldReturnInternalErrorResponse() throws Exception {
final var nullParam =
@ -168,6 +190,29 @@ public class PluginJsonRpcMethodTest extends JsonRpcHttpServiceTestBase {
}
});
}
if (input.toString().equals("data")) {
throw new PluginRpcEndpointException(
new RpcMethodError() {
@Override
public int getCode() {
return -2;
}
@Override
public String getMessage() {
return "Error with data";
}
@Override
public Optional<String> decodeData(final String data) {
// just turn everything uppercase
return Optional.of(data.toUpperCase(Locale.US));
}
},
"abc");
}
return input;
}
}

@ -69,7 +69,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = '0xiYCyr3M4oSrvqYXVkLgVDzlBg2T3fmrADub5tY5a0='
knownHash = '/FHIztl2tLW5Gzc0qnfEeuVQa6ljVfUce7YE6JLDdZU='
}
check.dependsOn('checkAPIChanges')

@ -20,6 +20,8 @@ import org.hyperledger.besu.plugin.services.rpc.RpcMethodError;
public class PluginRpcEndpointException extends RuntimeException {
/** The error */
private final RpcMethodError rpcMethodError;
/** The data associated with the exception */
private final String data;
/**
* Constructs a new PluginRpcEndpointException exception with the specified error.
@ -27,20 +29,18 @@ public class PluginRpcEndpointException extends RuntimeException {
* @param rpcMethodError the error.
*/
public PluginRpcEndpointException(final RpcMethodError rpcMethodError) {
super();
this.rpcMethodError = rpcMethodError;
this(rpcMethodError, null);
}
/**
* Constructs a new PluginRpcEndpointException exception with the specified error and message.
*
* @param rpcMethodError the error.
* @param message the detail message (which is saved for later retrieval by the {@link
* #getMessage()} method).
* @param data the data associated with the exception that could be parsed to extract more
* information to return in the error response.
*/
public PluginRpcEndpointException(final RpcMethodError rpcMethodError, final String message) {
super(message);
this.rpcMethodError = rpcMethodError;
public PluginRpcEndpointException(final RpcMethodError rpcMethodError, final String data) {
this(rpcMethodError, data, null);
}
/**
@ -48,16 +48,17 @@ public class PluginRpcEndpointException extends RuntimeException {
* cause.
*
* @param rpcMethodError the error.
* @param message the detail message (which is saved for later retrieval by the {@link
* #getMessage()} method).
* @param data the data associated with the exception that could be parsed to extract more
* information to return in the error response.
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
* (A {@code null} value is permitted, and indicates that the cause is nonexistent or
* unknown.)
*/
public PluginRpcEndpointException(
final RpcMethodError rpcMethodError, final String message, final Throwable cause) {
super(message, cause);
final RpcMethodError rpcMethodError, final String data, final Throwable cause) {
super(rpcMethodError.getMessage(), cause);
this.rpcMethodError = rpcMethodError;
this.data = data;
}
/**
@ -68,4 +69,13 @@ public class PluginRpcEndpointException extends RuntimeException {
public RpcMethodError getRpcMethodError() {
return rpcMethodError;
}
/**
* Get the data associated with the exception
*
* @return data as string, could be null.
*/
public String getData() {
return data;
}
}

Loading…
Cancel
Save