use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters in project besu by hyperledger.
the class PluginsReloadConfiguration method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
try {
final String pluginName = requestContext.getRequiredParameter(0, String.class);
if (!namedPlugins.containsKey(pluginName)) {
LOG.error("Plugin cannot be reloaded because no plugin has been registered with specified name: {}.", pluginName);
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.PLUGIN_NOT_FOUND);
}
reloadPluginConfig(namedPlugins.get(pluginName));
return new JsonRpcSuccessResponse(requestContext.getRequest().getId());
} catch (InvalidJsonRpcParameters invalidJsonRpcParameters) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters in project besu by hyperledger.
the class JsonRpcParameter method optional.
/**
* Retrieves an optional parameter at the given index interpreted as the given class. Throws
* InvalidJsonRpcParameters if parameter is of the wrong type.
*
* @param params the list of objects from which to extract a typed object.
* @param index Which index of the params array to access.
* @param paramClass What type is expected at this index.
* @param <T> The type of parameter.
* @return Returns the parameter cast as T if available.
*/
@SuppressWarnings("unchecked")
public <T> Optional<T> optional(final Object[] params, final int index, final Class<T> paramClass) {
if (params == null || params.length <= index || params[index] == null) {
return Optional.empty();
}
final T param;
final Object rawParam = params[index];
if (paramClass.isAssignableFrom(rawParam.getClass())) {
// If we're dealing with a simple type, just cast the value
param = (T) rawParam;
} else {
// Otherwise, serialize param back to json and then deserialize to the paramClass type
try {
final String json = mapper.writeValueAsString(rawParam);
param = mapper.readValue(json, paramClass);
} catch (final JsonProcessingException e) {
throw new InvalidJsonRpcParameters("Invalid json rpc parameter at index " + index, e);
}
}
return Optional.of(param);
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters in project besu by hyperledger.
the class WebSocketMessageHandlerTest method onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams.
@Test
public void onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams(final TestContext context) {
final Async async = context.async();
final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
final JsonRpcRequestContext expectedRequest = new JsonRpcRequestContext(requestJson.mapTo(WebSocketRpcRequest.class));
when(jsonRpcMethodMock.response(eq(expectedRequest))).thenThrow(new InvalidJsonRpcParameters(""));
final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(1, JsonRpcError.INVALID_PARAMS);
when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
handler.handle(websocketMock, requestJson.toBuffer(), Optional.empty());
async.awaitSuccess(WebSocketMessageHandlerTest.VERTX_AWAIT_TIMEOUT_MILLIS);
// can verify only after async not before
verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters in project besu by hyperledger.
the class AdminChangeLogLevel method response.
@Override
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
try {
final String rawLogLevel = requestContext.getRequiredParameter(0, String.class);
if (!VALID_PARAMS.contains(rawLogLevel)) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}
final Level logLevel = Level.toLevel(rawLogLevel);
final Optional<String[]> optionalLogFilters = requestContext.getOptionalParameter(1, String[].class);
optionalLogFilters.ifPresentOrElse(logFilters -> Arrays.stream(logFilters).forEach(logFilter -> setLogLevel(logFilter, logLevel)), () -> setLogLevel("", logLevel));
return new JsonRpcSuccessResponse(requestContext.getRequest().getId());
} catch (InvalidJsonRpcParameters invalidJsonRpcParameters) {
return new JsonRpcErrorResponse(requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS);
}
}
use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters in project besu by hyperledger.
the class WebSocketRequestHandlerTest method onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams.
@Test
public void onInvalidJsonRpcParametersExceptionProcessingRequestShouldRespondInvalidParams(final TestContext context) {
final Async async = context.async();
final JsonObject requestJson = new JsonObject().put("id", 1).put("method", "eth_x");
final JsonRpcRequestContext expectedRequest = new JsonRpcRequestContext(requestJson.mapTo(WebSocketRpcRequest.class));
when(jsonRpcMethodMock.response(eq(expectedRequest))).thenThrow(new InvalidJsonRpcParameters(""));
final JsonRpcErrorResponse expectedResponse = new JsonRpcErrorResponse(1, JsonRpcError.INVALID_PARAMS);
when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
handler.handle(websocketMock, requestJson.toString());
async.awaitSuccess(WebSocketRequestHandlerTest.VERTX_AWAIT_TIMEOUT_MILLIS);
// can verify only after async not before
verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
Aggregations