From 739467f5136c0e6f5404e8d89890697d2a111b2e Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Mon, 26 May 2025 13:29:29 +0800 Subject: [PATCH 1/9] JsonRpc:setTronErrorResolver --- .../core/services/jsonrpc/JsonRpcServlet.java | 1 + .../services/jsonrpc/TronErrorResolver.java | 77 +++++++++++++++++++ .../core/services/jsonrpc/TronJsonRpc.java | 2 +- .../services/jsonrpc/TronJsonRpcImpl.java | 3 +- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index 878b71d86b5..5ee515de2d0 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -43,6 +43,7 @@ public void init(ServletConfig config) throws ServletException { true); rpcServer = new JsonRpcServer(compositeService); + rpcServer.setErrorResolver(TronErrorResolver.INSTANCE); HttpStatusCodeProvider httpStatusCodeProvider = new HttpStatusCodeProvider() { @Override diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java new file mode 100644 index 00000000000..d2c2f3fa94e --- /dev/null +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java @@ -0,0 +1,77 @@ +package org.tron.core.services.jsonrpc; + +import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.ERROR_NOT_HANDLED; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorData; +import com.googlecode.jsonrpc4j.ErrorResolver; +import com.googlecode.jsonrpc4j.JsonRpcError; +import com.googlecode.jsonrpc4j.JsonRpcErrors; + +import com.googlecode.jsonrpc4j.ReflectionUtil; +import java.lang.reflect.Method; +import java.util.List; + +/** + * {@link ErrorResolver} that uses annotations. + */ +public enum TronErrorResolver implements ErrorResolver { + INSTANCE; + + /** + * {@inheritDoc} + */ + @Override + public JsonError resolveError( + Throwable thrownException, Method method, List arguments) { + JsonRpcError resolver = getResolverForException(thrownException, method); + if (notFoundResolver(resolver)) { + return null; + } + + String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); + String causeMessage + = thrownException.getCause() != null && thrownException.getCause().getMessage() != null + ? thrownException.getCause().getMessage() + : "{}"; + Object data = + hasErrorData(resolver) + ? resolver.data() + : causeMessage; + return new JsonError(resolver.code(), message, data); + } + + private JsonRpcError getResolverForException(Throwable thrownException, Method method) { + JsonRpcErrors errors = ReflectionUtil.getAnnotation(method, JsonRpcErrors.class); + if (hasAnnotations(errors)) { + for (JsonRpcError errorDefined : errors.value()) { + if (isExceptionInstanceOfError(thrownException, errorDefined)) { + return errorDefined; + } + } + } + return null; + } + + private boolean notFoundResolver(JsonRpcError resolver) { + return resolver == null; + } + + private boolean hasErrorMessage(JsonRpcError em) { + // noinspection ConstantConditions + return em.message() != null && !em.message().trim().isEmpty(); + } + + private boolean hasErrorData(JsonRpcError em) { + // noinspection ConstantConditions + return em.data() != null && !em.data().trim().isEmpty(); + } + + private boolean hasAnnotations(JsonRpcErrors errors) { + return errors != null; + } + + private boolean isExceptionInstanceOfError(Throwable target, JsonRpcError em) { + return em.exception().isInstance(target); + } +} \ No newline at end of file diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java index 52a3a2380d1..5143ddfc669 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java @@ -150,7 +150,7 @@ TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, Stri @JsonRpcErrors({ @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), - @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"), + @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000), }) String getCall(CallArguments transactionCall, Object blockNumOrTag) throws JsonRpcInvalidParamsException, JsonRpcInvalidRequestException, diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index eb432432a1c..510b0db83dc 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -483,7 +483,8 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va errMsg += ": " + msg; } - throw new JsonRpcInternalException(errMsg); + throw new JsonRpcInternalException(errMsg, + new ContractExeException("0x" + Hex.toHexString(resData))); } return result; From d9200bc3bbaed7e3d0b6b917d3c34f2df00eb306 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Mon, 26 May 2025 13:51:54 +0800 Subject: [PATCH 2/9] fix: add resData.length check while throw JsonRpcInternalException --- .../org/tron/core/services/jsonrpc/TronJsonRpcImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 510b0db83dc..a75f142045e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -483,8 +483,13 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va errMsg += ": " + msg; } - throw new JsonRpcInternalException(errMsg, - new ContractExeException("0x" + Hex.toHexString(resData))); + if (resData.length > 0) { + throw new JsonRpcInternalException(errMsg, + new ContractExeException("0x" + Hex.toHexString(resData))); + } else { + throw new JsonRpcInternalException(errMsg); + } + } return result; From 0d924f68dc9a839105c04e399cccb13149e764ea Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Wed, 28 May 2025 13:53:26 +0800 Subject: [PATCH 3/9] add data field in TronException --- .../exception/JsonRpcInternalException.java | 4 ++++ .../tron/core/exception/TronException.java | 10 ++++++++ .../services/jsonrpc/TronErrorResolver.java | 17 ++++++------- .../core/services/jsonrpc/TronJsonRpc.java | 2 +- .../services/jsonrpc/TronJsonRpcImpl.java | 3 +-- .../core/exception/TronExceptionTest.java | 24 +++++++++++++++++++ 6 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 framework/src/test/java/org/tron/core/exception/TronExceptionTest.java diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java b/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java index 12310e67747..1cb3fb164f3 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java +++ b/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java @@ -13,4 +13,8 @@ public JsonRpcInternalException(String message) { public JsonRpcInternalException(String message, Throwable cause) { super(message, cause); } + + public JsonRpcInternalException(String message, Object data) { + super(message, data); + } } \ No newline at end of file diff --git a/common/src/main/java/org/tron/core/exception/TronException.java b/common/src/main/java/org/tron/core/exception/TronException.java index 0447b28969e..831a1021319 100644 --- a/common/src/main/java/org/tron/core/exception/TronException.java +++ b/common/src/main/java/org/tron/core/exception/TronException.java @@ -1,12 +1,22 @@ package org.tron.core.exception; +import lombok.Getter; + +@Getter public class TronException extends Exception { + private Object data = null; public TronException() { super(); report(); } + public TronException(String message, Object data) { + super(message); + this.data = data; + report(); + } + public TronException(String message) { super(message); report(); diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java index d2c2f3fa94e..f118852c51e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java @@ -11,6 +11,7 @@ import com.googlecode.jsonrpc4j.ReflectionUtil; import java.lang.reflect.Method; import java.util.List; +import org.tron.core.exception.TronException; /** * {@link ErrorResolver} that uses annotations. @@ -30,14 +31,14 @@ public JsonError resolveError( } String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); - String causeMessage - = thrownException.getCause() != null && thrownException.getCause().getMessage() != null - ? thrownException.getCause().getMessage() - : "{}"; - Object data = - hasErrorData(resolver) - ? resolver.data() - : causeMessage; + Object data = hasErrorData(resolver) + ? resolver.data() + : new ErrorData(resolver.exception().getName(), message); + + if (thrownException instanceof TronException + && ((TronException)thrownException).getData() != null) { + data = ((TronException)thrownException).getData(); + } return new JsonError(resolver.code(), message, data); } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java index 5143ddfc669..52a3a2380d1 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java @@ -150,7 +150,7 @@ TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, Stri @JsonRpcErrors({ @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), - @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000), + @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"), }) String getCall(CallArguments transactionCall, Object blockNumOrTag) throws JsonRpcInvalidParamsException, JsonRpcInvalidRequestException, diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index a75f142045e..36d08b064ac 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -484,8 +484,7 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va } if (resData.length > 0) { - throw new JsonRpcInternalException(errMsg, - new ContractExeException("0x" + Hex.toHexString(resData))); + throw new JsonRpcInternalException(errMsg, "0x" + Hex.toHexString(resData)); } else { throw new JsonRpcInternalException(errMsg); } diff --git a/framework/src/test/java/org/tron/core/exception/TronExceptionTest.java b/framework/src/test/java/org/tron/core/exception/TronExceptionTest.java new file mode 100644 index 00000000000..15cc22cf24a --- /dev/null +++ b/framework/src/test/java/org/tron/core/exception/TronExceptionTest.java @@ -0,0 +1,24 @@ +package org.tron.core.exception; + +import org.junit.Assert; +import org.junit.Test; + +public class TronExceptionTest { + @Test + public void testTronExceptionWithData() { + String testData = "test_data"; + TronException exception = new TronException("test message", testData); + Assert.assertEquals(testData, exception.getData()); + + String hexData = "0x1234"; + JsonRpcInternalException rpcException = new JsonRpcInternalException("test", hexData); + Assert.assertEquals(hexData, rpcException.getData()); + + try { + throw new JsonRpcInternalException("test", hexData); + } catch (Exception e) { + Assert.assertTrue(e instanceof TronException); + Assert.assertEquals(hexData, ((TronException)e).getData()); + } + } +} From 63bd97c9fe01f92015ad5ef68755d1b9639006a1 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Thu, 12 Jun 2025 14:22:28 +0800 Subject: [PATCH 4/9] feat(api): rename JsonRpcErrorResolver, add unit test --- ...esolver.java => JsonRpcErrorResolver.java} | 5 +-- .../core/services/jsonrpc/JsonRpcServlet.java | 2 +- .../services/jsonrpc/TronJsonRpcImpl.java | 2 +- .../jsonrpc/JsonRpcErrorResolverTest.java | 43 +++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) rename framework/src/main/java/org/tron/core/services/jsonrpc/{TronErrorResolver.java => JsonRpcErrorResolver.java} (94%) create mode 100644 framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java similarity index 94% rename from framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java rename to framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java index f118852c51e..0b410eca4eb 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java @@ -1,13 +1,10 @@ package org.tron.core.services.jsonrpc; -import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.ERROR_NOT_HANDLED; - import com.fasterxml.jackson.databind.JsonNode; import com.googlecode.jsonrpc4j.ErrorData; import com.googlecode.jsonrpc4j.ErrorResolver; import com.googlecode.jsonrpc4j.JsonRpcError; import com.googlecode.jsonrpc4j.JsonRpcErrors; - import com.googlecode.jsonrpc4j.ReflectionUtil; import java.lang.reflect.Method; import java.util.List; @@ -16,7 +13,7 @@ /** * {@link ErrorResolver} that uses annotations. */ -public enum TronErrorResolver implements ErrorResolver { +public enum JsonRpcErrorResolver implements ErrorResolver { INSTANCE; /** diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java index 5ee515de2d0..104a0e9e470 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcServlet.java @@ -43,7 +43,7 @@ public void init(ServletConfig config) throws ServletException { true); rpcServer = new JsonRpcServer(compositeService); - rpcServer.setErrorResolver(TronErrorResolver.INSTANCE); + rpcServer.setErrorResolver(JsonRpcErrorResolver.INSTANCE); HttpStatusCodeProvider httpStatusCodeProvider = new HttpStatusCodeProvider() { @Override diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 36d08b064ac..f501deb8faa 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -484,7 +484,7 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va } if (resData.length > 0) { - throw new JsonRpcInternalException(errMsg, "0x" + Hex.toHexString(resData)); + throw new JsonRpcInternalException(errMsg, ByteArray.toJsonHex(resData)); } else { throw new JsonRpcInternalException(errMsg); } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java new file mode 100644 index 00000000000..c03a198f54c --- /dev/null +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java @@ -0,0 +1,43 @@ +package org.tron.core.services.jsonrpc; + +import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorResolver.JsonError; +import com.googlecode.jsonrpc4j.JsonRpcError; +import com.googlecode.jsonrpc4j.JsonRpcErrors; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; +import org.tron.core.exception.TronException; + +public class JsonRpcErrorResolverTest { + + private final JsonRpcErrorResolver resolver = JsonRpcErrorResolver.INSTANCE; + private final int errorCode = -32000; + + @JsonRpcErrors({ + @JsonRpcError(exception = TronException.class, code = errorCode, data = "{}") + }) + public void dummyMethod() { + } + + @Test + public void testResolveErrorWithTronException() throws Exception { + + String message = "JsonRPC ErrorMessage"; + String data = "JsonRPC ErrorData"; + + TronException exception = new TronException(message, data); + Method method = this.getClass().getMethod("dummyMethod"); + List arguments = new ArrayList<>(); + + JsonError error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(errorCode, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals(data, error.data); + } + +} \ No newline at end of file From 1c27a8b688487dc591c7df6cd63ffb453ef5c122 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Fri, 13 Jun 2025 12:08:00 +0800 Subject: [PATCH 5/9] feat(api): add note and test --- .../jsonrpc/JsonRpcErrorResolver.java | 2 + .../jsonrpc/JsonRpcErrorResolverTest.java | 44 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java index 0b410eca4eb..5b8bcd42090 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java @@ -28,6 +28,8 @@ public JsonError resolveError( } String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); + + // data priority: exception > annotation > default ErrorData Object data = hasErrorData(resolver) ? resolver.data() : new ErrorData(resolver.exception().getName(), message); diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java index c03a198f54c..407c5bd366c 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java @@ -1,6 +1,7 @@ package org.tron.core.services.jsonrpc; import com.fasterxml.jackson.databind.JsonNode; +import com.googlecode.jsonrpc4j.ErrorData; import com.googlecode.jsonrpc4j.ErrorResolver.JsonError; import com.googlecode.jsonrpc4j.JsonRpcError; import com.googlecode.jsonrpc4j.JsonRpcErrors; @@ -9,15 +10,20 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; +import org.tron.core.exception.JsonRpcInternalException; +import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.JsonRpcInvalidRequestException; import org.tron.core.exception.TronException; public class JsonRpcErrorResolverTest { private final JsonRpcErrorResolver resolver = JsonRpcErrorResolver.INSTANCE; - private final int errorCode = -32000; @JsonRpcErrors({ - @JsonRpcError(exception = TronException.class, code = errorCode, data = "{}") + @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), + @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), + @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"), + @JsonRpcError(exception = TronException.class, code = -1) }) public void dummyMethod() { } @@ -25,19 +31,45 @@ public void dummyMethod() { @Test public void testResolveErrorWithTronException() throws Exception { - String message = "JsonRPC ErrorMessage"; - String data = "JsonRPC ErrorData"; + String message = "JsonRpcInvalidRequestException"; - TronException exception = new TronException(message, data); + TronException exception = new JsonRpcInvalidRequestException(message); Method method = this.getClass().getMethod("dummyMethod"); List arguments = new ArrayList<>(); JsonError error = resolver.resolveError(exception, method, arguments); + Assert.assertNotNull(error); + Assert.assertEquals(-32600, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals("{}", error.data); + + message = "JsonRpcInternalException"; + String data = "JsonRpcInternalException data"; + exception = new JsonRpcInternalException(message, data); + error = resolver.resolveError(exception, method, arguments); Assert.assertNotNull(error); - Assert.assertEquals(errorCode, error.code); + Assert.assertEquals(-32000, error.code); Assert.assertEquals(message, error.message); Assert.assertEquals(data, error.data); + + exception = new JsonRpcInternalException(message, null); + error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(-32000, error.code); + Assert.assertEquals(message, error.message); + Assert.assertEquals("{}", error.data); + + message = "TronException"; + exception = new TronException(message, null); + error = resolver.resolveError(exception, method, arguments); + + Assert.assertNotNull(error); + Assert.assertEquals(-1, error.code); + Assert.assertEquals(message, error.message); + Assert.assertTrue(error.data instanceof ErrorData); + } } \ No newline at end of file From d5752417d2772b75f8d723ea07c8bbfdea6800d9 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Wed, 2 Jul 2025 00:08:29 +0800 Subject: [PATCH 6/9] feat(api): add JsonRpcException.java --- .../java/org/tron/common/utils/ByteArray.java | 2 +- .../tron/core/exception/TronException.java | 10 ------ .../exception/jsonrpc/JsonRpcException.java | 32 +++++++++++++++++++ .../JsonRpcInternalException.java | 4 +-- .../JsonRpcInvalidParamsException.java | 4 +-- .../JsonRpcInvalidRequestException.java | 4 +-- .../JsonRpcMethodNotFoundException.java | 4 +-- .../JsonRpcTooManyResultException.java | 4 +-- .../src/main/java/org/tron/core/Wallet.java | 2 +- .../core/services/jsonrpc/JsonRpcApiUtil.java | 2 +- .../jsonrpc/JsonRpcErrorResolver.java | 9 +++--- .../core/services/jsonrpc/TronJsonRpc.java | 10 +++--- .../services/jsonrpc/TronJsonRpcImpl.java | 13 +++++--- .../jsonrpc/filters/LogBlockQuery.java | 2 +- .../services/jsonrpc/filters/LogFilter.java | 2 +- .../jsonrpc/filters/LogFilterAndResult.java | 2 +- .../jsonrpc/filters/LogFilterWrapper.java | 2 +- .../services/jsonrpc/filters/LogMatch.java | 2 +- .../jsonrpc/types/BuildArguments.java | 4 +-- .../services/jsonrpc/types/CallArguments.java | 4 +-- .../tron/common/runtime/vm/Create2Test.java | 2 +- .../java/org/tron/core/CoreExceptionTest.java | 10 +++--- ...ionTest.java => JsonRpcExceptionTest.java} | 12 ++++--- .../org/tron/core/jsonrpc/JsonRpcTest.java | 2 +- .../tron/core/jsonrpc/JsonrpcServiceTest.java | 2 +- .../core/jsonrpc/LogMatchExactlyTest.java | 2 +- .../services/jsonrpc/BuildArgumentsTest.java | 4 +-- .../services/jsonrpc/CallArgumentsTest.java | 4 +-- .../jsonrpc/JsonRpcErrorResolverTest.java | 15 +++++---- 29 files changed, 100 insertions(+), 71 deletions(-) create mode 100644 common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInternalException.java (75%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInvalidParamsException.java (68%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcInvalidRequestException.java (69%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcMethodNotFoundException.java (68%) rename common/src/main/java/org/tron/core/exception/{ => jsonrpc}/JsonRpcTooManyResultException.java (69%) rename framework/src/test/java/org/tron/core/exception/{TronExceptionTest.java => JsonRpcExceptionTest.java} (53%) diff --git a/common/src/main/java/org/tron/common/utils/ByteArray.java b/common/src/main/java/org/tron/common/utils/ByteArray.java index b77dd310380..d0ac4cbaddf 100644 --- a/common/src/main/java/org/tron/common/utils/ByteArray.java +++ b/common/src/main/java/org/tron/common/utils/ByteArray.java @@ -14,7 +14,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; /* * Copyright (c) [2016] [ ] diff --git a/common/src/main/java/org/tron/core/exception/TronException.java b/common/src/main/java/org/tron/core/exception/TronException.java index 831a1021319..0447b28969e 100644 --- a/common/src/main/java/org/tron/core/exception/TronException.java +++ b/common/src/main/java/org/tron/core/exception/TronException.java @@ -1,22 +1,12 @@ package org.tron.core.exception; -import lombok.Getter; - -@Getter public class TronException extends Exception { - private Object data = null; public TronException() { super(); report(); } - public TronException(String message, Object data) { - super(message); - this.data = data; - report(); - } - public TronException(String message) { super(message); report(); diff --git a/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java new file mode 100644 index 00000000000..7ed42d101d5 --- /dev/null +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcException.java @@ -0,0 +1,32 @@ +package org.tron.core.exception.jsonrpc; + +import lombok.Getter; +import org.tron.core.exception.TronException; + +@Getter +public class JsonRpcException extends TronException { + private Object data = null; + + public JsonRpcException() { + super(); + report(); + } + + public JsonRpcException(String message, Object data) { + super(message); + this.data = data; + report(); + } + + public JsonRpcException(String message) { + super(message); + report(); + } + + public JsonRpcException(String message, Throwable cause) { + super(message, cause); + report(); + } + + +} diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java similarity index 75% rename from common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java index 1cb3fb164f3..904449866ae 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInternalException extends TronException { +public class JsonRpcInternalException extends JsonRpcException { public JsonRpcInternalException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java similarity index 68% rename from common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java index adf205a309a..55ee15521e1 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInvalidParamsException extends TronException { +public class JsonRpcInvalidParamsException extends JsonRpcException { public JsonRpcInvalidParamsException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java similarity index 69% rename from common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java index 2689bff0cd2..32bd11a3ed9 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcInvalidRequestException extends TronException { +public class JsonRpcInvalidRequestException extends JsonRpcException { public JsonRpcInvalidRequestException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java similarity index 68% rename from common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java index d8e18168d9d..406a51f45bd 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcMethodNotFoundException extends TronException { +public class JsonRpcMethodNotFoundException extends JsonRpcException { public JsonRpcMethodNotFoundException() { super(); diff --git a/common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java similarity index 69% rename from common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java rename to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java index e8e183d49c1..03bc089b1c7 100644 --- a/common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java +++ b/common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java @@ -1,6 +1,6 @@ -package org.tron.core.exception; +package org.tron.core.exception.jsonrpc; -public class JsonRpcTooManyResultException extends TronException { +public class JsonRpcTooManyResultException extends JsonRpcException { public JsonRpcTooManyResultException() { super(); diff --git a/framework/src/main/java/org/tron/core/Wallet.java b/framework/src/main/java/org/tron/core/Wallet.java index 8dfb18331ff..0f8fceb7baa 100755 --- a/framework/src/main/java/org/tron/core/Wallet.java +++ b/framework/src/main/java/org/tron/core/Wallet.java @@ -177,7 +177,6 @@ import org.tron.core.exception.DupTransactionException; import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInvalidParamsException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.exception.PermissionException; import org.tron.core.exception.SignatureFormatException; @@ -188,6 +187,7 @@ import org.tron.core.exception.VMIllegalException; import org.tron.core.exception.ValidateSignatureException; import org.tron.core.exception.ZksnarkException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.net.TronNetDelegate; import org.tron.core.net.TronNetService; import org.tron.core.net.message.adv.TransactionMessage; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java index 955ba55060f..4a60f14b534 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java @@ -26,7 +26,7 @@ import org.tron.common.utils.Sha256Hash; import org.tron.common.utils.StringUtil; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java index 5b8bcd42090..88b438c976e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java @@ -8,7 +8,7 @@ import com.googlecode.jsonrpc4j.ReflectionUtil; import java.lang.reflect.Method; import java.util.List; -import org.tron.core.exception.TronException; +import org.tron.core.exception.jsonrpc.JsonRpcException; /** * {@link ErrorResolver} that uses annotations. @@ -34,9 +34,10 @@ public JsonError resolveError( ? resolver.data() : new ErrorData(resolver.exception().getName(), message); - if (thrownException instanceof TronException - && ((TronException)thrownException).getData() != null) { - data = ((TronException)thrownException).getData(); + // Use data from JsonRpcException if present + if (thrownException instanceof JsonRpcException + && ((JsonRpcException)thrownException).getData() != null) { + data = ((JsonRpcException)thrownException).getData(); } return new JsonError(resolver.code(), message, data); } diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java index 52a3a2380d1..c078cecdb66 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java @@ -18,11 +18,11 @@ import org.tron.common.utils.ByteArray; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.jsonrpc.types.BlockResult; import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.CallArguments; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index f501deb8faa..dc8b7799c6f 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -57,12 +57,12 @@ import org.tron.core.exception.ContractValidateException; import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; import org.tron.core.exception.VMIllegalException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.http.JsonFormat; import org.tron.core.services.http.Util; @@ -474,8 +474,11 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va result = ByteArray.toJsonHex(listBytes); } else { logger.error("trigger contract failed."); + logger.error(retBuilder.build().toString()); String errMsg = retBuilder.getMessage().toStringUtf8(); byte[] resData = trxExtBuilder.getConstantResult(0).toByteArray(); + logger.info("res byteString" + trxExtBuilder.build().toString()); + logger.error(Hex.toHexString(resData)); if (resData.length > 4 && Hex.toHexString(resData).startsWith(ERROR_SELECTOR)) { String msg = ContractEventParser .parseDataBytes(org.bouncycastle.util.Arrays.copyOfRange(resData, 4, resData.length), diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogBlockQuery.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogBlockQuery.java index 7665c51106f..2c6773d8489 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogBlockQuery.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogBlockQuery.java @@ -10,7 +10,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.tron.common.bloom.Bloom; import org.tron.common.crypto.Hash; -import org.tron.core.exception.JsonRpcTooManyResultException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.store.SectionBloomStore; /** diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java index ce315e506d2..42bc123d4bc 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilter.java @@ -14,7 +14,7 @@ import org.tron.common.crypto.Hash; import org.tron.common.runtime.vm.DataWord; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.protos.Protocol.TransactionInfo.Log; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java index 3b893aec4cf..57739819d1e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterAndResult.java @@ -5,7 +5,7 @@ import java.util.concurrent.LinkedBlockingQueue; import lombok.Getter; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java index c0cd1ff12df..97a012b7f9a 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogFilterWrapper.java @@ -8,7 +8,7 @@ import org.tron.common.utils.ByteArray; import org.tron.core.Wallet; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.JsonRpcApiUtil; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.protos.Protocol.Block; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java index d96aa07f9a4..2c3d3c2a52e 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/filters/LogMatch.java @@ -10,7 +10,7 @@ import org.tron.core.db.Manager; import org.tron.core.exception.BadItemException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; import org.tron.protos.Protocol.TransactionInfo; import org.tron.protos.Protocol.TransactionInfo.Log; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java index 223e807e622..490219a13d9 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/BuildArguments.java @@ -14,8 +14,8 @@ import org.apache.commons.lang3.StringUtils; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.SmartContractOuterClass.SmartContract; diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java b/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java index 1485448c4b6..70edd1ad94f 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/types/CallArguments.java @@ -13,8 +13,8 @@ import org.apache.commons.lang3.StringUtils; import org.tron.api.GrpcAPI.BytesMessage; import org.tron.core.Wallet; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.protos.Protocol.Transaction.Contract.ContractType; import org.tron.protos.contract.SmartContractOuterClass.SmartContract; diff --git a/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java b/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java index f400b3215ee..6fa2801c51f 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/Create2Test.java @@ -19,9 +19,9 @@ import org.tron.core.Wallet; import org.tron.core.exception.ContractExeException; import org.tron.core.exception.ContractValidateException; -import org.tron.core.exception.JsonRpcInvalidParamsException; import org.tron.core.exception.ReceiptCheckErrException; import org.tron.core.exception.VMIllegalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.jsonrpc.TronJsonRpcImpl; import org.tron.core.vm.config.ConfigLoader; diff --git a/framework/src/test/java/org/tron/core/CoreExceptionTest.java b/framework/src/test/java/org/tron/core/CoreExceptionTest.java index 89feaba338c..f82b0efe326 100644 --- a/framework/src/test/java/org/tron/core/CoreExceptionTest.java +++ b/framework/src/test/java/org/tron/core/CoreExceptionTest.java @@ -29,11 +29,6 @@ import org.tron.core.exception.HeaderNotFound; import org.tron.core.exception.HighFreqException; import org.tron.core.exception.ItemNotFoundException; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; -import org.tron.core.exception.JsonRpcMethodNotFoundException; -import org.tron.core.exception.JsonRpcTooManyResultException; import org.tron.core.exception.NonCommonBlockException; import org.tron.core.exception.NonUniqueObjectException; import org.tron.core.exception.P2pException; @@ -56,6 +51,11 @@ import org.tron.core.exception.ValidateSignatureException; import org.tron.core.exception.ZkProofValidateException; import org.tron.core.exception.ZksnarkException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcMethodNotFoundException; +import org.tron.core.exception.jsonrpc.JsonRpcTooManyResultException; public class CoreExceptionTest { diff --git a/framework/src/test/java/org/tron/core/exception/TronExceptionTest.java b/framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java similarity index 53% rename from framework/src/test/java/org/tron/core/exception/TronExceptionTest.java rename to framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java index 15cc22cf24a..470f701429f 100644 --- a/framework/src/test/java/org/tron/core/exception/TronExceptionTest.java +++ b/framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java @@ -2,12 +2,14 @@ import org.junit.Assert; import org.junit.Test; +import org.tron.core.exception.jsonrpc.JsonRpcException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; -public class TronExceptionTest { +public class JsonRpcExceptionTest { @Test - public void testTronExceptionWithData() { + public void testJsonRpcExceptionWithData() { String testData = "test_data"; - TronException exception = new TronException("test message", testData); + JsonRpcException exception = new JsonRpcException("test message", testData); Assert.assertEquals(testData, exception.getData()); String hexData = "0x1234"; @@ -17,8 +19,8 @@ public void testTronExceptionWithData() { try { throw new JsonRpcInternalException("test", hexData); } catch (Exception e) { - Assert.assertTrue(e instanceof TronException); - Assert.assertEquals(hexData, ((TronException)e).getData()); + Assert.assertTrue(e instanceof JsonRpcException); + Assert.assertEquals(hexData, ((JsonRpcException)e).getData()); } } } diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java index bef0b5a1593..bd357101da3 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonRpcTest.java @@ -18,7 +18,7 @@ import org.tron.common.utils.ByteArray; import org.tron.common.utils.ByteUtil; import org.tron.common.utils.Commons; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.JsonRpcApiUtil; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.filters.LogBlockQuery; diff --git a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java index 0f2214c5c9c..fe22c7d5d93 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java @@ -35,7 +35,7 @@ import org.tron.core.capsule.TransactionCapsule; import org.tron.core.capsule.utils.BlockUtil; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.NodeInfoService; import org.tron.core.services.interfaceJsonRpcOnPBFT.JsonRpcServiceOnPBFT; import org.tron.core.services.interfaceJsonRpcOnSolidity.JsonRpcServiceOnSolidity; diff --git a/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java b/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java index 600cc52b58e..f55e3bc2cfa 100644 --- a/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java +++ b/framework/src/test/java/org/tron/core/jsonrpc/LogMatchExactlyTest.java @@ -9,7 +9,7 @@ import org.tron.common.runtime.vm.DataWord; import org.tron.common.runtime.vm.LogInfo; import org.tron.common.utils.ByteArray; -import org.tron.core.exception.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; import org.tron.core.services.jsonrpc.TronJsonRpc.FilterRequest; import org.tron.core.services.jsonrpc.TronJsonRpc.LogFilterElement; import org.tron.core.services.jsonrpc.filters.LogFilter; diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java index f9e264c515f..952e9c81467 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/BuildArgumentsTest.java @@ -8,8 +8,8 @@ import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.core.services.jsonrpc.types.BuildArguments; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.protos.Protocol; diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java index 19dd76e5e07..1d7f568453b 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/CallArgumentsTest.java @@ -8,8 +8,8 @@ import org.tron.core.Constant; import org.tron.core.Wallet; import org.tron.core.config.args.Args; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; import org.tron.core.services.jsonrpc.types.CallArguments; import org.tron.protos.Protocol; diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java index 407c5bd366c..364277a76e5 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java @@ -10,10 +10,11 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import org.tron.core.exception.JsonRpcInternalException; -import org.tron.core.exception.JsonRpcInvalidParamsException; -import org.tron.core.exception.JsonRpcInvalidRequestException; import org.tron.core.exception.TronException; +import org.tron.core.exception.jsonrpc.JsonRpcException; +import org.tron.core.exception.jsonrpc.JsonRpcInternalException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; +import org.tron.core.exception.jsonrpc.JsonRpcInvalidRequestException; public class JsonRpcErrorResolverTest { @@ -23,7 +24,7 @@ public class JsonRpcErrorResolverTest { @JsonRpcError(exception = JsonRpcInvalidRequestException.class, code = -32600, data = "{}"), @JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"), @JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}"), - @JsonRpcError(exception = TronException.class, code = -1) + @JsonRpcError(exception = JsonRpcException.class, code = -1) }) public void dummyMethod() { } @@ -33,7 +34,7 @@ public void testResolveErrorWithTronException() throws Exception { String message = "JsonRpcInvalidRequestException"; - TronException exception = new JsonRpcInvalidRequestException(message); + JsonRpcException exception = new JsonRpcInvalidRequestException(message); Method method = this.getClass().getMethod("dummyMethod"); List arguments = new ArrayList<>(); @@ -61,8 +62,8 @@ public void testResolveErrorWithTronException() throws Exception { Assert.assertEquals(message, error.message); Assert.assertEquals("{}", error.data); - message = "TronException"; - exception = new TronException(message, null); + message = "JsonRpcException"; + exception = new JsonRpcException(message, null); error = resolver.resolveError(exception, method, arguments); Assert.assertNotNull(error); From af80922e2c26ef953950d2dea5b58661cf9d3cf5 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Wed, 2 Jul 2025 16:27:55 +0800 Subject: [PATCH 7/9] feat(api): remove logs and unnecessary test case --- .../services/jsonrpc/TronJsonRpcImpl.java | 4 --- .../core/exception/JsonRpcExceptionTest.java | 26 ------------------- 2 files changed, 30 deletions(-) delete mode 100644 framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index dc8b7799c6f..2f6ca6d9c2a 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -473,12 +473,8 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va } result = ByteArray.toJsonHex(listBytes); } else { - logger.error("trigger contract failed."); - logger.error(retBuilder.build().toString()); String errMsg = retBuilder.getMessage().toStringUtf8(); byte[] resData = trxExtBuilder.getConstantResult(0).toByteArray(); - logger.info("res byteString" + trxExtBuilder.build().toString()); - logger.error(Hex.toHexString(resData)); if (resData.length > 4 && Hex.toHexString(resData).startsWith(ERROR_SELECTOR)) { String msg = ContractEventParser .parseDataBytes(org.bouncycastle.util.Arrays.copyOfRange(resData, 4, resData.length), diff --git a/framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java b/framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java deleted file mode 100644 index 470f701429f..00000000000 --- a/framework/src/test/java/org/tron/core/exception/JsonRpcExceptionTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.tron.core.exception; - -import org.junit.Assert; -import org.junit.Test; -import org.tron.core.exception.jsonrpc.JsonRpcException; -import org.tron.core.exception.jsonrpc.JsonRpcInternalException; - -public class JsonRpcExceptionTest { - @Test - public void testJsonRpcExceptionWithData() { - String testData = "test_data"; - JsonRpcException exception = new JsonRpcException("test message", testData); - Assert.assertEquals(testData, exception.getData()); - - String hexData = "0x1234"; - JsonRpcInternalException rpcException = new JsonRpcInternalException("test", hexData); - Assert.assertEquals(hexData, rpcException.getData()); - - try { - throw new JsonRpcInternalException("test", hexData); - } catch (Exception e) { - Assert.assertTrue(e instanceof JsonRpcException); - Assert.assertEquals(hexData, ((JsonRpcException)e).getData()); - } - } -} From 694445bfa4e1b37c84e7fe0be2da768c6f3ca6d2 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Thu, 3 Jul 2025 14:28:11 +0800 Subject: [PATCH 8/9] feat(api): enhance estimateGas error handling to include data field --- .../org/tron/core/services/jsonrpc/TronJsonRpcImpl.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index 2f6ca6d9c2a..ce9fc479ea5 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -648,7 +648,12 @@ public String estimateGas(CallArguments args) throws JsonRpcInvalidRequestExcept errMsg += ": " + msg; } - throw new JsonRpcInternalException(errMsg); + if (data.length > 0) { + throw new JsonRpcInternalException(errMsg, ByteArray.toJsonHex(data)); + } else { + throw new JsonRpcInternalException(errMsg); + } + } else { if (supportEstimateEnergy) { From 63504259aecf2c76dbbc825cbf61f0783efc1c69 Mon Sep 17 00:00:00 2001 From: 0xbigapple Date: Thu, 7 Aug 2025 16:41:47 +0800 Subject: [PATCH 9/9] refactor: optimize JsonRpcErrorResolver error data resolution --- .../services/jsonrpc/JsonRpcErrorResolver.java | 17 ++++++++++------- .../jsonrpc/JsonRpcErrorResolverTest.java | 1 - 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java index 88b438c976e..b92b3cf1af6 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java @@ -30,15 +30,18 @@ public JsonError resolveError( String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage(); // data priority: exception > annotation > default ErrorData - Object data = hasErrorData(resolver) - ? resolver.data() - : new ErrorData(resolver.exception().getName(), message); + Object data = null; + if (thrownException instanceof JsonRpcException) { + JsonRpcException jsonRpcException = (JsonRpcException) thrownException; + data = jsonRpcException.getData(); + } - // Use data from JsonRpcException if present - if (thrownException instanceof JsonRpcException - && ((JsonRpcException)thrownException).getData() != null) { - data = ((JsonRpcException)thrownException).getData(); + if (data == null) { + data = hasErrorData(resolver) + ? resolver.data() + : new ErrorData(resolver.exception().getName(), message); } + return new JsonError(resolver.code(), message, data); } diff --git a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java index 364277a76e5..d8e64308ab8 100644 --- a/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java +++ b/framework/src/test/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolverTest.java @@ -10,7 +10,6 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import org.tron.core.exception.TronException; import org.tron.core.exception.jsonrpc.JsonRpcException; import org.tron.core.exception.jsonrpc.JsonRpcInternalException; import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;