Skip to content

Commit 63bd97c

Browse files
committed
feat(api): rename JsonRpcErrorResolver, add unit test
1 parent 0d924f6 commit 63bd97c

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

framework/src/main/java/org/tron/core/services/jsonrpc/TronErrorResolver.java renamed to framework/src/main/java/org/tron/core/services/jsonrpc/JsonRpcErrorResolver.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package org.tron.core.services.jsonrpc;
22

3-
import static com.googlecode.jsonrpc4j.ErrorResolver.JsonError.ERROR_NOT_HANDLED;
4-
53
import com.fasterxml.jackson.databind.JsonNode;
64
import com.googlecode.jsonrpc4j.ErrorData;
75
import com.googlecode.jsonrpc4j.ErrorResolver;
86
import com.googlecode.jsonrpc4j.JsonRpcError;
97
import com.googlecode.jsonrpc4j.JsonRpcErrors;
10-
118
import com.googlecode.jsonrpc4j.ReflectionUtil;
129
import java.lang.reflect.Method;
1310
import java.util.List;
@@ -16,7 +13,7 @@
1613
/**
1714
* {@link ErrorResolver} that uses annotations.
1815
*/
19-
public enum TronErrorResolver implements ErrorResolver {
16+
public enum JsonRpcErrorResolver implements ErrorResolver {
2017
INSTANCE;
2118

2219
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void init(ServletConfig config) throws ServletException {
4343
true);
4444

4545
rpcServer = new JsonRpcServer(compositeService);
46-
rpcServer.setErrorResolver(TronErrorResolver.INSTANCE);
46+
rpcServer.setErrorResolver(JsonRpcErrorResolver.INSTANCE);
4747

4848
HttpStatusCodeProvider httpStatusCodeProvider = new HttpStatusCodeProvider() {
4949
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va
484484
}
485485

486486
if (resData.length > 0) {
487-
throw new JsonRpcInternalException(errMsg, "0x" + Hex.toHexString(resData));
487+
throw new JsonRpcInternalException(errMsg, ByteArray.toJsonHex(resData));
488488
} else {
489489
throw new JsonRpcInternalException(errMsg);
490490
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.tron.core.services.jsonrpc;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.googlecode.jsonrpc4j.ErrorResolver.JsonError;
5+
import com.googlecode.jsonrpc4j.JsonRpcError;
6+
import com.googlecode.jsonrpc4j.JsonRpcErrors;
7+
import java.lang.reflect.Method;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
import org.tron.core.exception.TronException;
13+
14+
public class JsonRpcErrorResolverTest {
15+
16+
private final JsonRpcErrorResolver resolver = JsonRpcErrorResolver.INSTANCE;
17+
private final int errorCode = -32000;
18+
19+
@JsonRpcErrors({
20+
@JsonRpcError(exception = TronException.class, code = errorCode, data = "{}")
21+
})
22+
public void dummyMethod() {
23+
}
24+
25+
@Test
26+
public void testResolveErrorWithTronException() throws Exception {
27+
28+
String message = "JsonRPC ErrorMessage";
29+
String data = "JsonRPC ErrorData";
30+
31+
TronException exception = new TronException(message, data);
32+
Method method = this.getClass().getMethod("dummyMethod");
33+
List<JsonNode> arguments = new ArrayList<>();
34+
35+
JsonError error = resolver.resolveError(exception, method, arguments);
36+
37+
Assert.assertNotNull(error);
38+
Assert.assertEquals(errorCode, error.code);
39+
Assert.assertEquals(message, error.message);
40+
Assert.assertEquals(data, error.data);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)