Skip to content

Commit 2893283

Browse files
committed
Added better handling of communication errors
1 parent ec0cf80 commit 2893283

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

‎src/main/java/io/api/etherscan/error/ConnectionException.java

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
public class ConnectionException extends ApiException {
1010

11+
public ConnectionException(String message) {
12+
super(message);
13+
}
14+
1115
public ConnectionException(String message, Throwable cause) {
1216
super(message, cause);
1317
}

‎src/main/java/io/api/etherscan/executor/impl/HttpExecutor.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.util.zip.GZIPInputStream;
1919
import java.util.zip.InflaterInputStream;
2020

21-
import static java.net.HttpURLConnection.HTTP_MOVED_PERM;
22-
import static java.net.HttpURLConnection.HTTP_MOVED_TEMP;
21+
import static java.net.HttpURLConnection.*;
2322

2423
/**
2524
* Http client implementation
@@ -88,6 +87,10 @@ public String get(final String urlAsString) {
8887
final int status = connection.getResponseCode();
8988
if (status == HTTP_MOVED_TEMP || status == HTTP_MOVED_PERM) {
9089
return get(connection.getHeaderField("Location"));
90+
} else if ((status >= HTTP_BAD_REQUEST) && (status < HTTP_INTERNAL_ERROR)) {
91+
throw new ConnectionException("Protocol error: "+connection.getResponseMessage());
92+
} else if (status >= HTTP_INTERNAL_ERROR) {
93+
throw new ConnectionException("Server error: "+connection.getResponseMessage());
9194
}
9295

9396
final String data = readData(connection);

0 commit comments

Comments
 (0)