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()); final Object result = function.apply(() -> request.getRequest().getParams());
return new JsonRpcSuccessResponse(request.getRequest().getId(), result); return new JsonRpcSuccessResponse(request.getRequest().getId(), result);
} catch (final PluginRpcEndpointException ex) { } catch (final PluginRpcEndpointException ex) {
final JsonRpcError error = new JsonRpcError(ex.getRpcMethodError(), ex.getMessage()); final JsonRpcError error = new JsonRpcError(ex.getRpcMethodError(), ex.getData());
LOG.error("Error calling plugin JSON-RPC endpoint", ex); LOG.debug("Error calling plugin JSON-RPC endpoint", ex);
return new JsonRpcErrorResponse(request.getRequest().getId(), error); return new JsonRpcErrorResponse(request.getRequest().getId(), error);
} }
} }

@ -37,8 +37,16 @@ public class JsonRpcTestHelper {
} }
protected void assertValidJsonRpcError( protected void assertValidJsonRpcError(
final JsonObject json, final Object id, final int errorCode, final String errorMessage) final JsonObject json, final Object id, final int errorCode, final String errorMessage) {
throws Exception { 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 // Check all expected fieldnames are set
final Set<String> fieldNames = json.fieldNames(); final Set<String> fieldNames = json.fieldNames();
assertThat(fieldNames.size()).isEqualTo(3); assertThat(fieldNames.size()).isEqualTo(3);
@ -53,13 +61,19 @@ public class JsonRpcTestHelper {
// Check error format // Check error format
final JsonObject error = json.getJsonObject("error"); final JsonObject error = json.getJsonObject("error");
final Set<String> errorFieldNames = error.fieldNames(); 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("code")).isTrue();
assertThat(errorFieldNames.contains("message")).isTrue(); assertThat(errorFieldNames.contains("message")).isTrue();
if (data != null) {
assertThat(errorFieldNames.contains("data")).isTrue();
}
// Check error field values // Check error field values
assertThat(error.getInteger("code")).isEqualTo(errorCode); assertThat(error.getInteger("code")).isEqualTo(errorCode);
assertThat(error.getString("message")).isEqualTo(errorMessage); assertThat(error.getString("message")).isEqualTo(errorMessage);
if (data != null) {
assertThat(error.getString("data")).isEqualTo(data);
}
} }
protected void assertIdMatches(final JsonObject json, final Object expectedId) { 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.PluginRpcRequest;
import org.hyperledger.besu.plugin.services.rpc.RpcMethodError; import org.hyperledger.besu.plugin.services.rpc.RpcMethodError;
import java.util.Locale;
import java.util.Optional;
import io.vertx.core.json.JsonObject; import io.vertx.core.json.JsonObject;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import okhttp3.Response; 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 @Test
public void unhandledExceptionShouldReturnInternalErrorResponse() throws Exception { public void unhandledExceptionShouldReturnInternalErrorResponse() throws Exception {
final var nullParam = 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; return input;
} }
} }

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

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

Loading…
Cancel
Save