diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/PluginJsonRpcMethod.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/PluginJsonRpcMethod.java index d5a5c0a81c..0366e078d6 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/PluginJsonRpcMethod.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/PluginJsonRpcMethod.java @@ -15,11 +15,14 @@ package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.INTERNAL_ERROR; +import static org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError.PLUGIN_INTERNAL_ERROR; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; +import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; +import org.hyperledger.besu.plugin.services.exception.PluginRpcEndpointException; import org.hyperledger.besu.plugin.services.rpc.PluginRpcRequest; import java.util.function.Function; @@ -47,9 +50,14 @@ public class PluginJsonRpcMethod implements JsonRpcMethod { @Override public JsonRpcResponse response(final JsonRpcRequestContext request) { try { - Object result = function.apply(() -> request.getRequest().getParams()); + final Object result = function.apply(() -> request.getRequest().getParams()); return new JsonRpcSuccessResponse(request.getRequest().getId(), result); - } catch (Exception ex) { + } catch (final PluginRpcEndpointException ex) { + final JsonRpcError error = PLUGIN_INTERNAL_ERROR; + error.setData(ex.getMessage()); + LOG.error("Error calling plugin JSON-RPC endpoint", ex); + return new JsonRpcErrorResponse(request.getRequest().getId(), error); + } catch (final Exception ex) { LOG.error("Error calling plugin JSON-RPC endpoint", ex); return new JsonRpcErrorResponse(request.getRequest().getId(), INTERNAL_ERROR); } diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/JsonRpcError.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/JsonRpcError.java index ef205ecea2..e7951d5acd 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/JsonRpcError.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/response/JsonRpcError.java @@ -213,6 +213,7 @@ public enum JsonRpcError { /** Plugins error */ PLUGIN_NOT_FOUND(-60000, "Plugin not found"), + PLUGIN_INTERNAL_ERROR(-32603, "Plugin internal error"), // Retesteth Errors diff --git a/plugin-api/build.gradle b/plugin-api/build.gradle index 0f371254fc..1c40a063b3 100644 --- a/plugin-api/build.gradle +++ b/plugin-api/build.gradle @@ -67,7 +67,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 = 'sMLZpz/Rie6f/uUFwxaDvVHU8F61pcLnazAfIVwyyH0=' + knownHash = '9FHwr8juEGZBU6+nCdHINFIJO7fetydF4AhMZzkOxIs=' } check.dependsOn('checkAPIChanges') diff --git a/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/exception/PluginRpcEndpointException.java b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/exception/PluginRpcEndpointException.java new file mode 100644 index 0000000000..cc8c2f01da --- /dev/null +++ b/plugin-api/src/main/java/org/hyperledger/besu/plugin/services/exception/PluginRpcEndpointException.java @@ -0,0 +1,25 @@ +/* + * Copyright Hyperledger Besu Contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.hyperledger.besu.plugin.services.exception; + +public class PluginRpcEndpointException extends RuntimeException { + public PluginRpcEndpointException(final String message) { + super(message); + } + + public PluginRpcEndpointException(final String message, final Throwable throwable) { + super(message, throwable); + } +}