Skip to content

Commit 5e16e13

Browse files
committed
When calling the underlying rpc interface to return an error, throw an exception containing the original response result
1 parent be6807a commit 5e16e13

6 files changed

Lines changed: 156 additions & 1 deletion

File tree

core/src/main/java/com/platon/contracts/ppos/BaseContract.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@
3030
import com.platon.tx.ReadonlyTransactionManager;
3131
import com.platon.tx.TransactionManager;
3232
import com.platon.tx.exceptions.ContractCallException;
33+
import com.platon.tx.exceptions.PlatonCallException;
34+
import com.platon.tx.exceptions.PlatonCallTimeoutException;
3335
import com.platon.tx.gas.ContractGasProvider;
3436
import com.platon.tx.gas.GasProvider;
3537
import com.platon.utils.JSONUtil;
3638
import com.platon.utils.Numeric;
39+
import com.platon.utils.Strings;
3740
import org.bouncycastle.util.encoders.Hex;
3841
import org.slf4j.Logger;
3942
import org.slf4j.LoggerFactory;
@@ -87,6 +90,20 @@ private <T> CallResponse<T> executeCallObjectValueReturn(Function function, Clas
8790
DefaultBlockParameterName.LATEST)
8891
.send();
8992

93+
//判断底层返回的错误信息是否包含超时信息
94+
if(ethCall.hasError()){
95+
Response.Error error = ethCall.getError();
96+
String message = error.getMessage();
97+
String lowMessage = !Strings.isBlank(message)? message.toLowerCase() : null;
98+
//包含timeout则抛超时异常,其他错误则直接抛出runtime异常
99+
if(!Strings.isBlank(lowMessage)
100+
&& lowMessage.contains("timeout")){
101+
throw new PlatonCallTimeoutException(error.getCode(),error.getMessage(),ethCall);
102+
} else {
103+
throw new PlatonCallException(error.getCode(),error.getMessage(),ethCall);
104+
}
105+
}
106+
90107
String result = Numeric.cleanHexPrefix(ethCall.getValue());
91108
if(result==null || "".equals(result)){
92109
throw new ContractCallException("Empty value (0x) returned from contract");
@@ -131,6 +148,20 @@ private <T> CallResponse<List<T>> executeCallListValueReturn(Function function,
131148
DefaultBlockParameterName.LATEST)
132149
.send();
133150

151+
//判断底层返回的错误信息是否包含超时信息
152+
if(ethCall.hasError()){
153+
Response.Error error = ethCall.getError();
154+
String message = error.getMessage();
155+
String lowMessage = !Strings.isBlank(message)? message.toLowerCase() : null;
156+
//包含timeout则抛超时异常,其他错误则直接抛出runtime异常
157+
if(!Strings.isBlank(lowMessage)
158+
&& lowMessage.contains("timeout")){
159+
throw new PlatonCallTimeoutException(error.getCode(),error.getMessage(),ethCall);
160+
} else {
161+
throw new PlatonCallException(error.getCode(),error.getMessage(),ethCall);
162+
}
163+
}
164+
134165
String result = Numeric.cleanHexPrefix(ethCall.getValue());
135166
if(result==null || "".equals(result)){
136167
throw new ContractCallException("Empty value (0x) returned from contract");

core/src/main/java/com/platon/protocol/websocket/WebSocketService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private void setRequestTimeout(long requestId) {
161161
executor.schedule(
162162
() -> closeRequest(
163163
requestId,
164-
new SocketTimeoutException(
164+
new IOException(
165165
String.format("Request with id %d timed out", requestId))),
166166
REQUEST_TIMEOUT,
167167
TimeUnit.SECONDS);

core/src/main/java/com/platon/tx/Contract.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
import com.platon.protocol.core.DefaultBlockParameter;
1111
import com.platon.protocol.core.DefaultBlockParameterName;
1212
import com.platon.protocol.core.RemoteCall;
13+
import com.platon.protocol.core.Response;
1314
import com.platon.protocol.core.methods.request.Transaction;
1415
import com.platon.protocol.core.methods.response.Log;
1516
import com.platon.protocol.core.methods.response.PlatonCall;
1617
import com.platon.protocol.core.methods.response.PlatonGetCode;
1718
import com.platon.protocol.core.methods.response.TransactionReceipt;
1819
import com.platon.protocol.exceptions.TransactionException;
1920
import com.platon.tx.exceptions.ContractCallException;
21+
import com.platon.tx.exceptions.PlatonCallException;
22+
import com.platon.tx.exceptions.PlatonCallTimeoutException;
2023
import com.platon.tx.gas.DefaultGasProvider;
2124
import com.platon.tx.gas.GasProvider;
2225
import com.platon.utils.Numeric;
26+
import com.platon.utils.Strings;
2327

2428
import java.io.IOException;
2529
import java.lang.reflect.Constructor;
@@ -149,6 +153,20 @@ private List<Type> executeCall(
149153
defaultBlockParameter)
150154
.send();
151155

156+
//判断底层返回的错误信息是否包含超时信息
157+
if(ethCall.hasError()){
158+
Response.Error error = ethCall.getError();
159+
String message = error.getMessage();
160+
String lowMessage = !Strings.isBlank(message)? message.toLowerCase() : null;
161+
//包含timeout则抛超时异常,其他错误则直接抛出runtime异常
162+
if(!Strings.isBlank(lowMessage)
163+
&& lowMessage.contains("timeout")){
164+
throw new PlatonCallTimeoutException(error.getCode(),error.getMessage(),ethCall);
165+
} else {
166+
throw new PlatonCallException(error.getCode(),error.getMessage(),ethCall);
167+
}
168+
}
169+
152170
String value = ethCall.getValue();
153171
return FunctionReturnDecoder.decode(value, function.getOutputParameters());
154172
}

core/src/main/java/com/platon/tx/WasmContract.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.platon.protocol.core.DefaultBlockParameter;
1010
import com.platon.protocol.core.DefaultBlockParameterName;
1111
import com.platon.protocol.core.RemoteCall;
12+
import com.platon.protocol.core.Response;
1213
import com.platon.protocol.core.methods.request.Transaction;
1314
import com.platon.protocol.core.methods.response.Log;
1415
import com.platon.protocol.core.methods.response.PlatonCall;
@@ -19,8 +20,11 @@
1920
import com.platon.rlp.wasm.RLPList;
2021
import com.platon.rlp.wasm.datatypes.Int;
2122
import com.platon.rlp.wasm.datatypes.Uint;
23+
import com.platon.tx.exceptions.PlatonCallException;
24+
import com.platon.tx.exceptions.PlatonCallTimeoutException;
2225
import com.platon.tx.gas.GasProvider;
2326
import com.platon.utils.Numeric;
27+
import com.platon.utils.Strings;
2428

2529
import java.io.IOException;
2630
import java.lang.reflect.Constructor;
@@ -127,6 +131,20 @@ protected <T> T executeCall(WasmFunction function, Class<T> clazz) throws IOExce
127131
defaultBlockParameter)
128132
.send();
129133

134+
//判断底层返回的错误信息是否包含超时信息
135+
if(ethCall.hasError()){
136+
Response.Error error = ethCall.getError();
137+
String message = error.getMessage();
138+
String lowMessage = !Strings.isBlank(message)? message.toLowerCase() : null;
139+
//包含timeout则抛超时异常,其他错误则直接抛出runtime异常
140+
if(!Strings.isBlank(lowMessage)
141+
&& lowMessage.contains("timeout")){
142+
throw new PlatonCallTimeoutException(error.getCode(),error.getMessage(),ethCall);
143+
} else {
144+
throw new PlatonCallException(error.getCode(),error.getMessage(),ethCall);
145+
}
146+
}
147+
130148
String value = ethCall.getValue();
131149
if (null != function.getOutputParameterizedType()) {
132150
return WasmReturnDecoder.decode(value, clazz, function.getOutputParameterizedType());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.platon.tx.exceptions;
2+
3+
import com.platon.protocol.core.Response;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @Author liushuyu
9+
* @Date 2021/5/12 16:46
10+
* @Version
11+
* @Desc
12+
*/
13+
public class PlatonCallException extends RuntimeException {
14+
15+
private Response response;
16+
private int code;
17+
private String msg;
18+
19+
public PlatonCallException(int code, String msg, Response response){
20+
this.code = code;
21+
this.msg = msg;
22+
this.response = response;
23+
}
24+
25+
public PlatonCallException(int code, String msg, Response response, Throwable ex){
26+
super(ex);
27+
this.code = code;
28+
this.msg = msg;
29+
this.response = response;
30+
}
31+
32+
public Response getResponse() {
33+
return response;
34+
}
35+
36+
public int getCode() {
37+
return code;
38+
}
39+
40+
public String getMsg() {
41+
return msg;
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.platon.tx.exceptions;
2+
3+
import com.platon.protocol.core.Response;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @Author liushuyu
9+
* @Date 2021/5/12 16:46
10+
* @Version
11+
* @Desc
12+
*/
13+
public class PlatonCallTimeoutException extends IOException {
14+
15+
private Response response;
16+
private int code;
17+
private String msg;
18+
19+
public PlatonCallTimeoutException(int code,String msg,Response response){
20+
this.code = code;
21+
this.msg = msg;
22+
this.response = response;
23+
}
24+
25+
public PlatonCallTimeoutException(int code,String msg,Response response,Throwable ex){
26+
super(ex);
27+
this.code = code;
28+
this.msg = msg;
29+
this.response = response;
30+
}
31+
32+
public Response getResponse() {
33+
return response;
34+
}
35+
36+
public int getCode() {
37+
return code;
38+
}
39+
40+
public String getMsg() {
41+
return msg;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)