Skip to content

Commit f5b2edb

Browse files
committed
[2.0.0-SNAPSHOT]
EtherScanLogQueryException name fixed EtherScanResponseException response entity added StringResponseTO#builder added BasicProvider error handling improved
1 parent 3210c39 commit f5b2edb

16 files changed

+160
-79
lines changed

src/main/java/io/goodforgod/api/etherscan/BasicProvider.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
abstract class BasicProvider {
2222

23+
private static final String MAX_RATE_LIMIT_REACHED = "Max rate limit reached";
24+
2325
static final int MAX_END_BLOCK = Integer.MAX_VALUE;
2426
static final int MIN_START_BLOCK = 0;
2527

@@ -46,17 +48,26 @@ abstract class BasicProvider {
4648
<T> T convert(byte[] json, Class<T> tClass) {
4749
try {
4850
final T t = converter.fromJson(json, tClass);
49-
if (t instanceof StringResponseTO && ((StringResponseTO) t).getResult().startsWith("Max rate limit reached")) {
51+
if (t instanceof StringResponseTO && ((StringResponseTO) t).getResult().startsWith(MAX_RATE_LIMIT_REACHED)) {
5052
throw new EtherScanRateLimitException(((StringResponseTO) t).getResult());
5153
}
5254

5355
return t;
5456
} catch (Exception e) {
57+
final StringResponseTO response = converter.fromJson(json, StringResponseTO.class);
58+
if (response.getResult() != null && response.getStatus() == 0) {
59+
if (response.getResult().startsWith(MAX_RATE_LIMIT_REACHED)) {
60+
throw new EtherScanRateLimitException(response.getResult());
61+
} else {
62+
throw new EtherScanResponseException(response);
63+
}
64+
}
65+
5566
final String jsonAsString = new String(json, StandardCharsets.UTF_8);
5667
try {
5768
final Map<String, Object> map = converter.fromJson(json, Map.class);
5869
final Object result = map.get("result");
59-
if (result instanceof String && ((String) result).startsWith("Max rate limit reached"))
70+
if (result instanceof String && ((String) result).startsWith(MAX_RATE_LIMIT_REACHED))
6071
throw new EtherScanRateLimitException(((String) result));
6172

6273
throw new EtherScanParseException(e.getMessage() + ", for response: " + jsonAsString, e.getCause(), jsonAsString);
@@ -72,8 +83,15 @@ byte[] getRequest(String urlParameters) {
7283
queue.takeTurn();
7384
final URI uri = URI.create(baseUrl + module + urlParameters);
7485
final byte[] result = executor.get(uri);
75-
if (result.length == 0)
76-
throw new EtherScanResponseException("Server returned null value for GET request at URL - " + uri);
86+
if (result.length == 0) {
87+
final StringResponseTO emptyResponse = StringResponseTO.builder()
88+
.withStatus("0")
89+
.withMessage("Server returned null value for GET request at URL - " + uri)
90+
.withResult("")
91+
.build();
92+
93+
throw new EtherScanResponseException(emptyResponse, "Server returned null value for GET request at URL - " + uri);
94+
}
7795

7896
return result;
7997
}

src/main/java/io/goodforgod/api/etherscan/BlockAPIProvider.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.goodforgod.api.etherscan;
22

33
import io.goodforgod.api.etherscan.error.EtherScanException;
4+
import io.goodforgod.api.etherscan.error.EtherScanResponseException;
45
import io.goodforgod.api.etherscan.http.EthHttpClient;
56
import io.goodforgod.api.etherscan.manager.RequestQueueManager;
67
import io.goodforgod.api.etherscan.model.BlockUncle;
@@ -12,8 +13,8 @@
1213
/**
1314
* Block API Implementation
1415
*
15-
* @see BlockAPI
1616
* @author GoodforGod
17+
* @see BlockAPI
1718
* @since 28.10.2018
1819
*/
1920
final class BlockAPIProvider extends BasicProvider implements BlockAPI {
@@ -34,17 +35,26 @@ final class BlockAPIProvider extends BasicProvider implements BlockAPI {
3435
public Optional<BlockUncle> uncles(long blockNumber) throws EtherScanException {
3536
final String urlParam = ACT_BLOCK_PARAM + BLOCKNO_PARAM + blockNumber;
3637
final byte[] response = getRequest(urlParam);
37-
if (response.length == 0)
38-
return Optional.empty();
39-
40-
final UncleBlockResponseTO responseTO = convert(response, UncleBlockResponseTO.class);
41-
if (responseTO.getMessage().equals("NOTOK")) {
38+
if (response.length == 0) {
4239
return Optional.empty();
4340
}
4441

45-
BasicUtils.validateTxResponse(responseTO);
46-
return (responseTO.getResult() == null || responseTO.getResult().isEmpty())
47-
? Optional.empty()
48-
: Optional.of(responseTO.getResult());
42+
try {
43+
final UncleBlockResponseTO responseTO = convert(response, UncleBlockResponseTO.class);
44+
if (responseTO.getMessage().startsWith("NOTOK")) {
45+
return Optional.empty();
46+
}
47+
48+
BasicUtils.validateTxResponse(responseTO);
49+
return (responseTO.getResult() == null || responseTO.getResult().isEmpty())
50+
? Optional.empty()
51+
: Optional.of(responseTO.getResult());
52+
} catch (EtherScanResponseException e) {
53+
if (e.getResponse().getMessage().startsWith("NOTOK")) {
54+
return Optional.empty();
55+
} else {
56+
throw e;
57+
}
58+
}
4959
}
5060
}

src/main/java/io/goodforgod/api/etherscan/ContractAPIProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Abi contractAbi(String address) throws EtherScanException {
3636

3737
final String urlParam = ACT_ABI_PARAM + ADDRESS_PARAM + address;
3838
final StringResponseTO response = getRequest(urlParam, StringResponseTO.class);
39-
if (response.getStatus() != 1 && "NOTOK".equals(response.getMessage())) {
39+
if (response.getStatus() != 1 && response.getMessage().startsWith("NOTOK")) {
4040
throw new EtherScanResponseException(response);
4141
}
4242

src/main/java/io/goodforgod/api/etherscan/EtherScanAPI.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public interface EtherScanAPI extends AutoCloseable {
3737
@NotNull
3838
GasTrackerAPI gasTracker();
3939

40-
@NotNull
41-
static EtherScanAPI build() {
42-
return builder().build();
43-
}
44-
4540
@NotNull
4641
static Builder builder() {
4742
return new EthScanAPIBuilder();

src/main/java/io/goodforgod/api/etherscan/ProxyAPIProvider.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.goodforgod.api.etherscan.model.proxy.utility.StringProxyTO;
1414
import io.goodforgod.api.etherscan.model.proxy.utility.TxInfoProxyTO;
1515
import io.goodforgod.api.etherscan.model.proxy.utility.TxProxyTO;
16+
import io.goodforgod.api.etherscan.model.response.StringResponseTO;
1617
import io.goodforgod.api.etherscan.util.BasicUtils;
1718
import java.util.Optional;
1819
import java.util.regex.Pattern;
@@ -142,10 +143,17 @@ public Optional<String> txSendRaw(String hexEncodedTx) throws EtherScanException
142143

143144
final String urlParams = ACT_SEND_RAW_TX_PARAM + HEX_PARAM + hexEncodedTx;
144145
final StringProxyTO response = postRequest(urlParams, "", StringProxyTO.class);
145-
if (response.getError() != null)
146-
throw new EtherScanResponseException("Error occurred with code " + response.getError().getCode()
146+
if (response.getError() != null) {
147+
final StringResponseTO responseError = StringResponseTO.builder()
148+
.withStatus("0")
149+
.withMessage(response.getError().getMessage())
150+
.withResult(response.getError().getCode())
151+
.build();
152+
153+
throw new EtherScanResponseException(responseError, "Error occurred with code " + response.getError().getCode()
147154
+ " with message " + response.getError().getMessage()
148155
+ ", error id " + response.getId() + ", jsonRPC " + response.getJsonrpc());
156+
}
149157

150158
return Optional.ofNullable(response.getResult());
151159
}

src/main/java/io/goodforgod/api/etherscan/error/ErtherScanLogQueryException.java renamed to src/main/java/io/goodforgod/api/etherscan/error/EtherScanLogQueryException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* @author GoodforGod
55
* @since 31.10.2018
66
*/
7-
public class ErtherScanLogQueryException extends EtherScanException {
7+
public class EtherScanLogQueryException extends EtherScanException {
88

9-
public ErtherScanLogQueryException(String message) {
9+
public EtherScanLogQueryException(String message) {
1010
super(message);
1111
}
1212
}

src/main/java/io/goodforgod/api/etherscan/error/EtherScanResponseException.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99
*/
1010
public class EtherScanResponseException extends EtherScanException {
1111

12+
private final transient BaseResponseTO response;
13+
1214
public EtherScanResponseException(BaseResponseTO response) {
13-
this(response.getMessage() + ", with status: " + response.getStatus());
15+
this(response, response.getMessage() + ", with status: " + response.getStatus());
1416
}
1517

1618
public EtherScanResponseException(StringResponseTO response) {
17-
this(response.getResult() + ", with status: " + response.getStatus() + ", with message: " + response.getMessage());
19+
this(response,
20+
response.getResult() + ", with status: " + response.getStatus() + ", with message: " + response.getMessage());
1821
}
1922

20-
public EtherScanResponseException(String message) {
23+
public EtherScanResponseException(BaseResponseTO response, String message) {
2124
super(message);
25+
this.response = response;
26+
}
27+
28+
public BaseResponseTO getResponse() {
29+
return response;
2230
}
2331
}

src/main/java/io/goodforgod/api/etherscan/model/query/LogQueryBuilderImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.goodforgod.api.etherscan.model.query;
22

33
import io.goodforgod.api.etherscan.LogsAPI;
4-
import io.goodforgod.api.etherscan.error.ErtherScanLogQueryException;
4+
import io.goodforgod.api.etherscan.error.EtherScanLogQueryException;
55
import io.goodforgod.api.etherscan.util.BasicUtils;
66
import org.jetbrains.annotations.NotNull;
77

@@ -40,47 +40,47 @@ final class LogQueryBuilderImpl implements LogQuery.Builder {
4040
@Override
4141
public @NotNull LogTopicSingle withTopic(@NotNull String topic0) {
4242
if (BasicUtils.isNotHex(topic0))
43-
throw new ErtherScanLogQueryException("topic0 can not be empty or non hex.");
43+
throw new EtherScanLogQueryException("topic0 can not be empty or non hex.");
4444
return new LogTopicSingle(address, startBlock, endBlock, topic0);
4545
}
4646

4747
@Override
4848
public @NotNull LogTopicTuple withTopic(@NotNull String topic0, @NotNull String topic1) {
4949
if (BasicUtils.isNotHex(topic0))
50-
throw new ErtherScanLogQueryException("topic0 can not be empty or non hex.");
50+
throw new EtherScanLogQueryException("topic0 can not be empty or non hex.");
5151
if (BasicUtils.isNotHex(topic1))
52-
throw new ErtherScanLogQueryException("topic1 can not be empty or non hex.");
52+
throw new EtherScanLogQueryException("topic1 can not be empty or non hex.");
5353
return new LogTopicTuple(address, startBlock, endBlock, topic0, topic1);
5454
}
5555

5656
@Override
5757
public @NotNull LogTopicTriple withTopic(@NotNull String topic0, @NotNull String topic1, @NotNull String topic2) {
5858
if (BasicUtils.isNotHex(topic0))
59-
throw new ErtherScanLogQueryException("topic0 can not be empty or non hex.");
59+
throw new EtherScanLogQueryException("topic0 can not be empty or non hex.");
6060
if (BasicUtils.isNotHex(topic1))
61-
throw new ErtherScanLogQueryException("topic1 can not be empty or non hex.");
61+
throw new EtherScanLogQueryException("topic1 can not be empty or non hex.");
6262
if (BasicUtils.isNotHex(topic2))
63-
throw new ErtherScanLogQueryException("topic2 can not be empty or non hex.");
63+
throw new EtherScanLogQueryException("topic2 can not be empty or non hex.");
6464
return new LogTopicTriple(address, startBlock, endBlock, topic0, topic1, topic2);
6565
}
6666

6767
@Override
6868
public @NotNull LogTopicQuadro
6969
withTopic(@NotNull String topic0, @NotNull String topic1, @NotNull String topic2, @NotNull String topic3) {
7070
if (BasicUtils.isNotHex(topic0))
71-
throw new ErtherScanLogQueryException("topic0 can not be empty or non hex.");
71+
throw new EtherScanLogQueryException("topic0 can not be empty or non hex.");
7272
if (BasicUtils.isNotHex(topic1))
73-
throw new ErtherScanLogQueryException("topic1 can not be empty or non hex.");
73+
throw new EtherScanLogQueryException("topic1 can not be empty or non hex.");
7474
if (BasicUtils.isNotHex(topic2))
75-
throw new ErtherScanLogQueryException("topic2 can not be empty or non hex.");
75+
throw new EtherScanLogQueryException("topic2 can not be empty or non hex.");
7676
if (BasicUtils.isNotHex(topic3))
77-
throw new ErtherScanLogQueryException("topic3 can not be empty or non hex.");
77+
throw new EtherScanLogQueryException("topic3 can not be empty or non hex.");
7878

7979
return new LogTopicQuadro(address, startBlock, endBlock, topic0, topic1, topic2, topic3);
8080
}
8181

8282
@Override
83-
public @NotNull LogQuery build() throws ErtherScanLogQueryException {
83+
public @NotNull LogQuery build() throws EtherScanLogQueryException {
8484
return new LogQueryImpl("&address=" + this.address + "&fromBlock=" + this.startBlock + "&toBlock=" + this.endBlock);
8585
}
8686
}

src/main/java/io/goodforgod/api/etherscan/model/query/LogTopicQuadro.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static io.goodforgod.api.etherscan.model.query.LogQueryParams.*;
44

55
import io.goodforgod.api.etherscan.LogsAPI;
6-
import io.goodforgod.api.etherscan.error.ErtherScanLogQueryException;
6+
import io.goodforgod.api.etherscan.error.EtherScanLogQueryException;
77
import org.jetbrains.annotations.NotNull;
88

99
/**
@@ -71,17 +71,17 @@ public LogTopicQuadro setOpTopic1_3(LogOp topic1_3_opr) {
7171
@Override
7272
public @NotNull LogQuery build() {
7373
if (topic0_1_opr == null)
74-
throw new ErtherScanLogQueryException("topic0_1_opr can not be null.");
74+
throw new EtherScanLogQueryException("topic0_1_opr can not be null.");
7575
if (topic0_2_opr == null)
76-
throw new ErtherScanLogQueryException("topic0_2_opr can not be null.");
76+
throw new EtherScanLogQueryException("topic0_2_opr can not be null.");
7777
if (topic0_3_opr == null)
78-
throw new ErtherScanLogQueryException("topic0_3_opr can not be null.");
78+
throw new EtherScanLogQueryException("topic0_3_opr can not be null.");
7979
if (topic1_2_opr == null)
80-
throw new ErtherScanLogQueryException("topic1_2_opr can not be null.");
80+
throw new EtherScanLogQueryException("topic1_2_opr can not be null.");
8181
if (topic2_3_opr == null)
82-
throw new ErtherScanLogQueryException("topic2_3_opr can not be null.");
82+
throw new EtherScanLogQueryException("topic2_3_opr can not be null.");
8383
if (topic1_3_opr == null)
84-
throw new ErtherScanLogQueryException("topic1_3_opr can not be null.");
84+
throw new EtherScanLogQueryException("topic1_3_opr can not be null.");
8585

8686
return new LogQueryImpl(ADDRESS_PARAM + address
8787
+ FROM_BLOCK_PARAM + startBlock + TO_BLOCK_PARAM + endBlock

src/main/java/io/goodforgod/api/etherscan/model/query/LogTopicSingle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static io.goodforgod.api.etherscan.model.query.LogQueryParams.*;
44

55
import io.goodforgod.api.etherscan.LogsAPI;
6-
import io.goodforgod.api.etherscan.error.ErtherScanLogQueryException;
6+
import io.goodforgod.api.etherscan.error.EtherScanLogQueryException;
77
import org.jetbrains.annotations.NotNull;
88

99
/**
@@ -29,7 +29,7 @@ public final class LogTopicSingle implements LogTopicBuilder {
2929
}
3030

3131
@Override
32-
public @NotNull LogQuery build() throws ErtherScanLogQueryException {
32+
public @NotNull LogQuery build() throws EtherScanLogQueryException {
3333
return new LogQueryImpl(ADDRESS_PARAM + address
3434
+ FROM_BLOCK_PARAM + startBlock + TO_BLOCK_PARAM + endBlock
3535
+ TOPIC_0_PARAM + topic0);

0 commit comments

Comments
 (0)