Skip to content

Commit c7f133d

Browse files
authored
feat(jsonrpc): jsonrpc set error resolver (#6369)
* JsonRpc:setTronErrorResolver * fix: add resData.length check while throw JsonRpcInternalException * add data field in TronException * feat(api): rename JsonRpcErrorResolver, add unit test * feat(api): add note and test * feat(api): add JsonRpcException.java * feat(api): remove logs and unnecessary test case * feat(api): enhance estimateGas error handling to include data field * refactor: optimize JsonRpcErrorResolver error data resolution
1 parent 6a75d50 commit c7f133d

28 files changed

+250
-48
lines changed

common/src/main/java/org/tron/common/utils/ByteArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.apache.commons.lang3.ArrayUtils;
1515
import org.apache.commons.lang3.StringUtils;
1616
import org.bouncycastle.util.encoders.Hex;
17-
import org.tron.core.exception.JsonRpcInvalidParamsException;
17+
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
1818

1919
/*
2020
* Copyright (c) [2016] [ <ether.camp> ]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.tron.core.exception.jsonrpc;
2+
3+
import lombok.Getter;
4+
import org.tron.core.exception.TronException;
5+
6+
@Getter
7+
public class JsonRpcException extends TronException {
8+
private Object data = null;
9+
10+
public JsonRpcException() {
11+
super();
12+
report();
13+
}
14+
15+
public JsonRpcException(String message, Object data) {
16+
super(message);
17+
this.data = data;
18+
report();
19+
}
20+
21+
public JsonRpcException(String message) {
22+
super(message);
23+
report();
24+
}
25+
26+
public JsonRpcException(String message, Throwable cause) {
27+
super(message, cause);
28+
report();
29+
}
30+
31+
32+
}

common/src/main/java/org/tron/core/exception/JsonRpcInternalException.java renamed to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInternalException.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.tron.core.exception;
1+
package org.tron.core.exception.jsonrpc;
22

3-
public class JsonRpcInternalException extends TronException {
3+
public class JsonRpcInternalException extends JsonRpcException {
44

55
public JsonRpcInternalException() {
66
super();
@@ -13,4 +13,8 @@ public JsonRpcInternalException(String message) {
1313
public JsonRpcInternalException(String message, Throwable cause) {
1414
super(message, cause);
1515
}
16+
17+
public JsonRpcInternalException(String message, Object data) {
18+
super(message, data);
19+
}
1620
}

common/src/main/java/org/tron/core/exception/JsonRpcInvalidParamsException.java renamed to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidParamsException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.tron.core.exception;
1+
package org.tron.core.exception.jsonrpc;
22

3-
public class JsonRpcInvalidParamsException extends TronException {
3+
public class JsonRpcInvalidParamsException extends JsonRpcException {
44

55
public JsonRpcInvalidParamsException() {
66
super();

common/src/main/java/org/tron/core/exception/JsonRpcInvalidRequestException.java renamed to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcInvalidRequestException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.tron.core.exception;
1+
package org.tron.core.exception.jsonrpc;
22

3-
public class JsonRpcInvalidRequestException extends TronException {
3+
public class JsonRpcInvalidRequestException extends JsonRpcException {
44

55
public JsonRpcInvalidRequestException() {
66
super();

common/src/main/java/org/tron/core/exception/JsonRpcMethodNotFoundException.java renamed to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcMethodNotFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.tron.core.exception;
1+
package org.tron.core.exception.jsonrpc;
22

3-
public class JsonRpcMethodNotFoundException extends TronException {
3+
public class JsonRpcMethodNotFoundException extends JsonRpcException {
44

55
public JsonRpcMethodNotFoundException() {
66
super();

common/src/main/java/org/tron/core/exception/JsonRpcTooManyResultException.java renamed to common/src/main/java/org/tron/core/exception/jsonrpc/JsonRpcTooManyResultException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package org.tron.core.exception;
1+
package org.tron.core.exception.jsonrpc;
22

3-
public class JsonRpcTooManyResultException extends TronException {
3+
public class JsonRpcTooManyResultException extends JsonRpcException {
44

55
public JsonRpcTooManyResultException() {
66
super();

framework/src/main/java/org/tron/core/Wallet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@
177177
import org.tron.core.exception.DupTransactionException;
178178
import org.tron.core.exception.HeaderNotFound;
179179
import org.tron.core.exception.ItemNotFoundException;
180-
import org.tron.core.exception.JsonRpcInvalidParamsException;
181180
import org.tron.core.exception.NonUniqueObjectException;
182181
import org.tron.core.exception.PermissionException;
183182
import org.tron.core.exception.SignatureFormatException;
@@ -188,6 +187,7 @@
188187
import org.tron.core.exception.VMIllegalException;
189188
import org.tron.core.exception.ValidateSignatureException;
190189
import org.tron.core.exception.ZksnarkException;
190+
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
191191
import org.tron.core.net.TronNetDelegate;
192192
import org.tron.core.net.TronNetService;
193193
import org.tron.core.net.message.adv.TransactionMessage;

framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcApiUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.tron.common.utils.Sha256Hash;
2727
import org.tron.common.utils.StringUtil;
2828
import org.tron.core.Wallet;
29-
import org.tron.core.exception.JsonRpcInvalidParamsException;
29+
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
3030
import org.tron.protos.Protocol.Block;
3131
import org.tron.protos.Protocol.Transaction;
3232
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.tron.core.services.jsonrpc;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.googlecode.jsonrpc4j.ErrorData;
5+
import com.googlecode.jsonrpc4j.ErrorResolver;
6+
import com.googlecode.jsonrpc4j.JsonRpcError;
7+
import com.googlecode.jsonrpc4j.JsonRpcErrors;
8+
import com.googlecode.jsonrpc4j.ReflectionUtil;
9+
import java.lang.reflect.Method;
10+
import java.util.List;
11+
import org.tron.core.exception.jsonrpc.JsonRpcException;
12+
13+
/**
14+
* {@link ErrorResolver} that uses annotations.
15+
*/
16+
public enum JsonRpcErrorResolver implements ErrorResolver {
17+
INSTANCE;
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
@Override
23+
public JsonError resolveError(
24+
Throwable thrownException, Method method, List<JsonNode> arguments) {
25+
JsonRpcError resolver = getResolverForException(thrownException, method);
26+
if (notFoundResolver(resolver)) {
27+
return null;
28+
}
29+
30+
String message = hasErrorMessage(resolver) ? resolver.message() : thrownException.getMessage();
31+
32+
// data priority: exception > annotation > default ErrorData
33+
Object data = null;
34+
if (thrownException instanceof JsonRpcException) {
35+
JsonRpcException jsonRpcException = (JsonRpcException) thrownException;
36+
data = jsonRpcException.getData();
37+
}
38+
39+
if (data == null) {
40+
data = hasErrorData(resolver)
41+
? resolver.data()
42+
: new ErrorData(resolver.exception().getName(), message);
43+
}
44+
45+
return new JsonError(resolver.code(), message, data);
46+
}
47+
48+
private JsonRpcError getResolverForException(Throwable thrownException, Method method) {
49+
JsonRpcErrors errors = ReflectionUtil.getAnnotation(method, JsonRpcErrors.class);
50+
if (hasAnnotations(errors)) {
51+
for (JsonRpcError errorDefined : errors.value()) {
52+
if (isExceptionInstanceOfError(thrownException, errorDefined)) {
53+
return errorDefined;
54+
}
55+
}
56+
}
57+
return null;
58+
}
59+
60+
private boolean notFoundResolver(JsonRpcError resolver) {
61+
return resolver == null;
62+
}
63+
64+
private boolean hasErrorMessage(JsonRpcError em) {
65+
// noinspection ConstantConditions
66+
return em.message() != null && !em.message().trim().isEmpty();
67+
}
68+
69+
private boolean hasErrorData(JsonRpcError em) {
70+
// noinspection ConstantConditions
71+
return em.data() != null && !em.data().trim().isEmpty();
72+
}
73+
74+
private boolean hasAnnotations(JsonRpcErrors errors) {
75+
return errors != null;
76+
}
77+
78+
private boolean isExceptionInstanceOfError(Throwable target, JsonRpcError em) {
79+
return em.exception().isInstance(target);
80+
}
81+
}

0 commit comments

Comments
 (0)