From 6581123550eb201f70de73a4759355e45a51c911 Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Wed, 10 Apr 2024 12:54:09 +0100 Subject: [PATCH] refactor(rpc): clean-up error handling --- .../rpc/exception/RskErrorResolver.java | 103 ++++++++---------- .../handlers/BaseRskErrorHandler.java | 58 ++++++++++ .../handlers/DefaultRskErrorHandler.java | 35 ++++++ .../InvalidFormatExceptionHandler.java | 35 ++++++ .../InvalidRskAddressExceptionHandler.java | 38 +++++++ .../handlers/JsonMappingExceptionHandler.java | 38 +++++++ .../exception/handlers/RskErrorHandler.java | 28 +++++ .../RskJsonRpcRequestExceptionHandler.java | 40 +++++++ .../UnrecognizedPropertyExceptionHandler.java | 39 +++++++ .../UnsupportedOperationExceptionHandler.java | 38 +++++++ .../rpc/exception/RskErrorResolverTest.java | 8 +- 11 files changed, 401 insertions(+), 59 deletions(-) create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/BaseRskErrorHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/DefaultRskErrorHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidFormatExceptionHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidRskAddressExceptionHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/JsonMappingExceptionHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskErrorHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskJsonRpcRequestExceptionHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnrecognizedPropertyExceptionHandler.java create mode 100644 rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnsupportedOperationExceptionHandler.java diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/RskErrorResolver.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/RskErrorResolver.java index b74bc17cb56..20b379a820c 100644 --- a/rskj-core/src/main/java/org/ethereum/rpc/exception/RskErrorResolver.java +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/RskErrorResolver.java @@ -1,87 +1,74 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ package org.ethereum.rpc.exception; import co.rsk.core.exception.InvalidRskAddressException; -import co.rsk.jsonrpc.JsonRpcError; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.googlecode.jsonrpc4j.ErrorResolver; +import org.ethereum.rpc.exception.handlers.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Method; -import java.util.Iterator; +import java.util.HashMap; import java.util.List; - -import static javax.xml.bind.DatatypeConverter.printHexBinary; +import java.util.Map; /** * Created by mario on 17/10/2016. */ public class RskErrorResolver implements ErrorResolver { + private final Map, Class> errorHandlers; - private static final Logger logger = LoggerFactory.getLogger("web3"); + public RskErrorResolver() { + errorHandlers = new HashMap<>(); + errorHandlers.put(InvalidRskAddressException.class, InvalidRskAddressExceptionHandler.class); + errorHandlers.put(RskJsonRpcRequestException.class, RskJsonRpcRequestExceptionHandler.class); + errorHandlers.put(InvalidFormatException.class, InvalidFormatExceptionHandler.class); + errorHandlers.put(UnrecognizedPropertyException.class, UnrecognizedPropertyExceptionHandler.class); + errorHandlers.put(UnsupportedOperationException.class, UnsupportedOperationExceptionHandler.class); + } @Override - public JsonError resolveError(Throwable t, Method method, List arguments) { - JsonError error; - - if (t instanceof InvalidRskAddressException) { - error = new JsonError( - JsonRpcError.INVALID_PARAMS, - "invalid argument 0: hex string has length " + arguments.get(0).asText().replace("0x", "").length() + ", want 40 for RSK address", - null); - } else if (t instanceof RskJsonRpcRequestException) { - RskJsonRpcRequestException rskJsonRpcRequestException = (RskJsonRpcRequestException) t; - byte[] revertData = rskJsonRpcRequestException.getRevertData(); - String errorDataHexString = "0x" + printHexBinary(revertData == null ? new byte[]{} : revertData).toLowerCase(); - error = new JsonError(rskJsonRpcRequestException.getCode(), t.getMessage(), errorDataHexString); - } else if (t instanceof InvalidFormatException) { - error = new JsonError(JsonRpcError.INTERNAL_ERROR, "Internal server error, probably due to invalid parameter type", null); - } else if (t instanceof UnrecognizedPropertyException) { - error = new JsonError( - JsonRpcError.INVALID_PARAMS, - getExceptionMessage((UnrecognizedPropertyException) t), - null); - } else if (t instanceof JsonMappingException && t.getMessage().contains("Can not construct instance")) { - error = new JsonError( - JsonRpcError.INVALID_PARAMS, - "invalid argument 0: json: cannot unmarshal string into value of input", - null); - } else if (t instanceof UnsupportedOperationException || (t.getMessage() != null && t.getMessage().toLowerCase().contains("method not supported"))) { - error = new JsonError( - JsonRpcError.METHOD_NOT_FOUND, - "the method " + method.getName() + " does not exist/is not available", - null); - } else { - logger.error("JsonRPC error when for method {} with arguments {}", method, arguments, t); - error = new JsonError(JsonRpcError.INTERNAL_ERROR, "Internal server error", null); + public JsonError resolveError(Throwable throwable, Method method, List arguments) { + if (throwable instanceof JsonMappingException && throwable.getMessage() != null && throwable.getMessage().contains("Can not construct instance")) { + return new JsonMappingExceptionHandler().apply(throwable, method, arguments); } - return error; - } - - private static String getExceptionMessage(UnrecognizedPropertyException ex) { - if (ex.getPropertyName() == null || ex.getKnownPropertyIds() == null) return "Invalid parameters"; - StringBuilder stringBuilder = new StringBuilder("Unrecognized field \""); - stringBuilder.append(ex.getPropertyName()); - stringBuilder.append("\" ("); - stringBuilder.append(ex.getKnownPropertyIds().size()); - stringBuilder.append(" known properties: ["); + if (throwable.getMessage() != null && throwable.getMessage().toLowerCase().contains("method not supported")) { + return new UnsupportedOperationExceptionHandler().apply(throwable, method, arguments); + } - Iterator iterator = ex.getKnownPropertyIds().iterator(); - while (iterator.hasNext()) { - stringBuilder.append("\""); - stringBuilder.append(iterator.next()); - stringBuilder.append("\""); - if (iterator.hasNext()) { - stringBuilder.append(", "); - } + Class handlerClass = errorHandlers.get(throwable.getClass()); + if (handlerClass == null) { + return (new DefaultRskErrorHandler()).apply(throwable, method, arguments); } - stringBuilder.append("])"); + RskErrorHandler handler; + try { + handler = handlerClass.getDeclaredConstructor().newInstance(); + } catch (Exception exception) { + return (new DefaultRskErrorHandler()).apply(throwable, method, arguments); + } - return stringBuilder.toString(); + return handler.apply(throwable, method, arguments); } } diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/BaseRskErrorHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/BaseRskErrorHandler.java new file mode 100644 index 00000000000..9e2ebe82126 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/BaseRskErrorHandler.java @@ -0,0 +1,58 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.util.Iterator; +import java.util.List; + +public abstract class BaseRskErrorHandler implements RskErrorHandler { + private static final Logger LOGGER = LoggerFactory.getLogger("web3"); + + protected void logJsonRPCError(Throwable throwable, Method method, List arguments) { + LOGGER.error("JsonRPC error for method {} with arguments {}", method, arguments, throwable); + } + + protected static String getExceptionMessage(UnrecognizedPropertyException ex) { + if (ex.getPropertyName() == null || ex.getKnownPropertyIds() == null) return "Invalid parameters"; + + StringBuilder stringBuilder = new StringBuilder("Unrecognized field \""); + stringBuilder.append(ex.getPropertyName()); + stringBuilder.append("\" ("); + stringBuilder.append(ex.getKnownPropertyIds().size()); + stringBuilder.append(" known properties: ["); + + Iterator iterator = ex.getKnownPropertyIds().iterator(); + while (iterator.hasNext()) { + stringBuilder.append("\""); + stringBuilder.append(iterator.next()); + stringBuilder.append("\""); + if (iterator.hasNext()) { + stringBuilder.append(", "); + } + } + stringBuilder.append("])"); + + return stringBuilder.toString(); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/DefaultRskErrorHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/DefaultRskErrorHandler.java new file mode 100644 index 00000000000..20d4d2c0999 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/DefaultRskErrorHandler.java @@ -0,0 +1,35 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class DefaultRskErrorHandler extends BaseRskErrorHandler { + + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + return new ErrorResolver.JsonError(JsonRpcError.INTERNAL_ERROR, "Internal server error", null); + }; +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidFormatExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidFormatExceptionHandler.java new file mode 100644 index 00000000000..82450dab541 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidFormatExceptionHandler.java @@ -0,0 +1,35 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class InvalidFormatExceptionHandler extends BaseRskErrorHandler { + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + + return new ErrorResolver.JsonError(JsonRpcError.INTERNAL_ERROR, "Internal server error, probably due to invalid parameter type", null); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidRskAddressExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidRskAddressExceptionHandler.java new file mode 100644 index 00000000000..f2b89ac1e5c --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/InvalidRskAddressExceptionHandler.java @@ -0,0 +1,38 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver.JsonError; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class InvalidRskAddressExceptionHandler extends BaseRskErrorHandler { + @Override + public JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + + return new JsonError( + JsonRpcError.INVALID_PARAMS, + "invalid argument 0: hex string has length " + arguments.get(0).asText().replace("0x", "").length() + ", want 40 for RSK address", + null); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/JsonMappingExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/JsonMappingExceptionHandler.java new file mode 100644 index 00000000000..6f5ed882693 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/JsonMappingExceptionHandler.java @@ -0,0 +1,38 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class JsonMappingExceptionHandler extends BaseRskErrorHandler { + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + + return new ErrorResolver.JsonError( + JsonRpcError.INVALID_PARAMS, + "invalid argument 0: json: cannot unmarshal string into value of input", + null); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskErrorHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskErrorHandler.java new file mode 100644 index 00000000000..1c32dbeac92 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskErrorHandler.java @@ -0,0 +1,28 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver.JsonError; + +import java.lang.reflect.Method; +import java.util.List; + +public interface RskErrorHandler { + JsonError apply(Throwable throwable, Method method, List arguments); +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskJsonRpcRequestExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskJsonRpcRequestExceptionHandler.java new file mode 100644 index 00000000000..7a018c5e532 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/RskJsonRpcRequestExceptionHandler.java @@ -0,0 +1,40 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.ethereum.rpc.exception.RskJsonRpcRequestException; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +import static javax.xml.bind.DatatypeConverter.printHexBinary; + +public class RskJsonRpcRequestExceptionHandler extends BaseRskErrorHandler { + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + RskJsonRpcRequestException rskJsonRpcRequestException = (RskJsonRpcRequestException) throwable; + byte[] revertData = rskJsonRpcRequestException.getRevertData(); + String errorDataHexString = "0x" + printHexBinary(revertData == null ? new byte[]{} : revertData).toLowerCase(); + + return new ErrorResolver.JsonError(rskJsonRpcRequestException.getCode(), throwable.getMessage(), errorDataHexString); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnrecognizedPropertyExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnrecognizedPropertyExceptionHandler.java new file mode 100644 index 00000000000..5d4ee7575e1 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnrecognizedPropertyExceptionHandler.java @@ -0,0 +1,39 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class UnrecognizedPropertyExceptionHandler extends BaseRskErrorHandler { + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + + return new ErrorResolver.JsonError( + JsonRpcError.INVALID_PARAMS, + getExceptionMessage((UnrecognizedPropertyException) throwable), + null); + } +} diff --git a/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnsupportedOperationExceptionHandler.java b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnsupportedOperationExceptionHandler.java new file mode 100644 index 00000000000..1c1156793a9 --- /dev/null +++ b/rskj-core/src/main/java/org/ethereum/rpc/exception/handlers/UnsupportedOperationExceptionHandler.java @@ -0,0 +1,38 @@ +/* + * This file is part of RskJ + * Copyright (C) 2018 RSK Labs Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package org.ethereum.rpc.exception.handlers; + +import co.rsk.jsonrpc.JsonRpcError; +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver; +import org.slf4j.Logger; + +import java.lang.reflect.Method; +import java.util.List; + +public class UnsupportedOperationExceptionHandler extends BaseRskErrorHandler { + @Override + public ErrorResolver.JsonError apply(Throwable throwable, Method method, List arguments) { + logJsonRPCError(throwable, method, arguments); + + return new ErrorResolver.JsonError( + JsonRpcError.METHOD_NOT_FOUND, + "the method " + method.getName() + " does not exist/is not available", + null); + } +} diff --git a/rskj-core/src/test/java/org/ethereum/rpc/exception/RskErrorResolverTest.java b/rskj-core/src/test/java/org/ethereum/rpc/exception/RskErrorResolverTest.java index 7a76a2cf54c..004f4b4e5c3 100644 --- a/rskj-core/src/test/java/org/ethereum/rpc/exception/RskErrorResolverTest.java +++ b/rskj-core/src/test/java/org/ethereum/rpc/exception/RskErrorResolverTest.java @@ -1,6 +1,7 @@ package org.ethereum.rpc.exception; import co.rsk.core.exception.InvalidRskAddressException; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.exc.InvalidFormatException; @@ -89,7 +90,12 @@ void test_resolveError_givenRskJsonRpcRequestExceptionWithData_returnsJsonErrorW @Test void test_resolveError_givenInvalidFormatException_returnsJsonErrorAsExpected() throws NoSuchMethodException { // Given - InvalidFormatException exception = mock(InvalidFormatException.class); + InvalidFormatException exception = new InvalidFormatException( + mock(JsonParser.class), + "", + new byte[]{}, + Exception.class + ); Method methodMock = this.getClass().getMethod("mockMethod"); List jsonNodeListMock = new ArrayList<>();