Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/main/java/io/ipinfo/api/errors/ErrorResponseException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
package io.ipinfo.api.errors;

public class ErrorResponseException extends RuntimeException {
public ErrorResponseException() {}

private final int statusCode;

public ErrorResponseException() {
this.statusCode = -1;
}

public ErrorResponseException(Exception ex) {
super(ex);
this.statusCode = -1;
}

public ErrorResponseException(int statusCode, String responseBody) {
super(
"Unexpected API response (status " +
statusCode +
"): " +
responseBody
);
this.statusCode = statusCode;
}

public int getStatusCode() {
return statusCode;
}
}
23 changes: 17 additions & 6 deletions src/main/java/io/ipinfo/api/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import okhttp3.Response;

public abstract class BaseRequest<T> {
protected final static Gson gson = new Gson();

protected static final Gson gson = new Gson();
private final OkHttpClient client;
private final String token;

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

public abstract T handle() throws RateLimitedException;

public Response handleRequest(Request.Builder request) throws RateLimitedException {
public Response handleRequest(Request.Builder request)
throws RateLimitedException {
request
.addHeader("Authorization", Credentials.basic(token, ""))
.addHeader("user-agent", "IPinfoClient/Java/3.3.0")
.addHeader("Content-Type", "application/json");
.addHeader("Authorization", Credentials.basic(token, ""))
.addHeader("user-agent", "IPinfoClient/Java/3.3.0")
.addHeader("Content-Type", "application/json");

Response response;

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

if (!response.isSuccessful()) {
String body = "";
try {
if (response.body() != null) {
body = response.body().string();
}
} catch (Exception ignored) {}
throw new ErrorResponseException(response.code(), body);
}

return response;
}
}

44 changes: 44 additions & 0 deletions src/test/java/io/ipinfo/IPinfoTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.ipinfo;

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.ErrorResponseException;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.ASNResponse;
import io.ipinfo.api.model.IPResponse;
Expand Down Expand Up @@ -388,4 +389,47 @@ public void testLookupResproxyEmpty() throws IOException {
server.shutdown();
}
}

@Test
public void testServerErrorThrowsErrorResponseException() throws IOException {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse()
.setResponseCode(503)
.setBody("Service Temporarily Unavailable")
.addHeader("Content-Type", "text/plain"));
server.start();

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(chain -> {
okhttp3.Request originalRequest = chain.request();
okhttp3.HttpUrl newUrl = originalRequest.url().newBuilder()
.scheme("http")
.host(server.getHostName())
.port(server.getPort())
.build();
okhttp3.Request newRequest = originalRequest.newBuilder()
.url(newUrl)
.build();
return chain.proceed(newRequest);
})
.build();

IPinfo ii = new IPinfo.Builder()
.setToken("test_token")
.setClient(client)
.build();

try {
ErrorResponseException ex = assertThrows(
ErrorResponseException.class,
() -> ii.lookupIP("8.8.8.8")
);
assertEquals(503, ex.getStatusCode());
assertTrue(ex.getMessage().contains("503"), "message should contain status code");
assertTrue(ex.getMessage().contains("Service Temporarily Unavailable"),
"message should contain response body");
} finally {
server.shutdown();
}
}
}
Loading