Skip to content

Commit 27f8988

Browse files
authored
Merge pull request #66 from ipinfo/silvano/eng-765-enhance-error-message-in-java-library-when-requests-fail
Show better error messages when requests fail
2 parents 2e1fb0a + dabb7e5 commit 27f8988

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
package io.ipinfo.api.errors;
22

33
public class ErrorResponseException extends RuntimeException {
4-
public ErrorResponseException() {}
4+
5+
private final int statusCode;
6+
7+
public ErrorResponseException() {
8+
this.statusCode = -1;
9+
}
510

611
public ErrorResponseException(Exception ex) {
712
super(ex);
13+
this.statusCode = -1;
14+
}
15+
16+
public ErrorResponseException(int statusCode, String responseBody) {
17+
super(
18+
"Unexpected API response (status " +
19+
statusCode +
20+
"): " +
21+
responseBody
22+
);
23+
this.statusCode = statusCode;
24+
}
25+
26+
public int getStatusCode() {
27+
return statusCode;
828
}
929
}

src/main/java/io/ipinfo/api/request/BaseRequest.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import okhttp3.Response;
1010

1111
public abstract class BaseRequest<T> {
12-
protected final static Gson gson = new Gson();
12+
13+
protected static final Gson gson = new Gson();
1314
private final OkHttpClient client;
1415
private final String token;
1516

@@ -20,11 +21,12 @@ protected BaseRequest(OkHttpClient client, String token) {
2021

2122
public abstract T handle() throws RateLimitedException;
2223

23-
public Response handleRequest(Request.Builder request) throws RateLimitedException {
24+
public Response handleRequest(Request.Builder request)
25+
throws RateLimitedException {
2426
request
25-
.addHeader("Authorization", Credentials.basic(token, ""))
26-
.addHeader("user-agent", "IPinfoClient/Java/3.3.0")
27-
.addHeader("Content-Type", "application/json");
27+
.addHeader("Authorization", Credentials.basic(token, ""))
28+
.addHeader("user-agent", "IPinfoClient/Java/3.3.0")
29+
.addHeader("Content-Type", "application/json");
2830

2931
Response response;
3032

@@ -43,7 +45,16 @@ public Response handleRequest(Request.Builder request) throws RateLimitedExcepti
4345
throw new RateLimitedException();
4446
}
4547

48+
if (!response.isSuccessful()) {
49+
String body = "";
50+
try {
51+
if (response.body() != null) {
52+
body = response.body().string();
53+
}
54+
} catch (Exception ignored) {}
55+
throw new ErrorResponseException(response.code(), body);
56+
}
57+
4658
return response;
4759
}
4860
}
49-

src/test/java/io/ipinfo/IPinfoTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.ipinfo;
22

33
import io.ipinfo.api.IPinfo;
4+
import io.ipinfo.api.errors.ErrorResponseException;
45
import io.ipinfo.api.errors.RateLimitedException;
56
import io.ipinfo.api.model.ASNResponse;
67
import io.ipinfo.api.model.IPResponse;
@@ -388,4 +389,47 @@ public void testLookupResproxyEmpty() throws IOException {
388389
server.shutdown();
389390
}
390391
}
392+
393+
@Test
394+
public void testServerErrorThrowsErrorResponseException() throws IOException {
395+
MockWebServer server = new MockWebServer();
396+
server.enqueue(new MockResponse()
397+
.setResponseCode(503)
398+
.setBody("Service Temporarily Unavailable")
399+
.addHeader("Content-Type", "text/plain"));
400+
server.start();
401+
402+
OkHttpClient client = new OkHttpClient.Builder()
403+
.addInterceptor(chain -> {
404+
okhttp3.Request originalRequest = chain.request();
405+
okhttp3.HttpUrl newUrl = originalRequest.url().newBuilder()
406+
.scheme("http")
407+
.host(server.getHostName())
408+
.port(server.getPort())
409+
.build();
410+
okhttp3.Request newRequest = originalRequest.newBuilder()
411+
.url(newUrl)
412+
.build();
413+
return chain.proceed(newRequest);
414+
})
415+
.build();
416+
417+
IPinfo ii = new IPinfo.Builder()
418+
.setToken("test_token")
419+
.setClient(client)
420+
.build();
421+
422+
try {
423+
ErrorResponseException ex = assertThrows(
424+
ErrorResponseException.class,
425+
() -> ii.lookupIP("8.8.8.8")
426+
);
427+
assertEquals(503, ex.getStatusCode());
428+
assertTrue(ex.getMessage().contains("503"), "message should contain status code");
429+
assertTrue(ex.getMessage().contains("Service Temporarily Unavailable"),
430+
"message should contain response body");
431+
} finally {
432+
server.shutdown();
433+
}
434+
}
391435
}

0 commit comments

Comments
 (0)