Skip to content

Commit f71dcb9

Browse files
committed
http: Better exception messages
1 parent c401d8d commit f71dcb9

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

http/src/main/java/gg/eventalerts/sdk/http/endpoint/EAEndpoint.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public EAAction<O> retrieveOne(@NotNull String... pathSegments) {
175175
}
176176

177177
@NotNull
178-
protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNull String objectField, @Nullable Map<String, Object> queryParams, @Nullable String... pathSegments) throws IOException {
178+
public String buildUrl(@NotNull String endpointPath, @Nullable Map<String, Object> queryParams, @Nullable String... pathSegments) {
179179
// Build query parameters
180180
final StringBuilder queryString = new StringBuilder();
181181
if (queryParams != null && !queryParams.isEmpty()) {
@@ -216,8 +216,14 @@ protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNul
216216
final StringBuilder path = new StringBuilder();
217217
if (pathSegments != null) for (final String segment : pathSegments) path.append("/").append(segment);
218218

219+
// Build and return full URL
220+
return http.url + endpointPath + path + queryString;
221+
}
222+
223+
@NotNull
224+
protected ConnectionDetails openConnection(@NotNull String url, @NotNull String objectField) throws IOException {
219225
// Open connection
220-
final HttpURLConnection connection = (HttpURLConnection) URI.create(http.url + endpointPath + path + queryString).toURL().openConnection();
226+
final HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
221227
connection.setRequestMethod("GET");
222228
for (final Map.Entry<String, String> header : http.headers.entrySet()) connection.setRequestProperty(header.getKey(), header.getValue());
223229

@@ -227,7 +233,7 @@ protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNul
227233
final JsonObject json = GSONProvider.GSON.fromJson(body, JsonObject.class);
228234
if (json == null) {
229235
if (statusCode >= 400) throw new EAHttpResponseException(statusCode, connection.getResponseMessage(), body);
230-
throw new EAHttpRequestException("GET " + endpointPath, new IllegalStateException("Failed to parse JSON response"));
236+
throw new EAHttpRequestException("GET " + url, new IllegalStateException("Failed to parse JSON response"));
231237
}
232238

233239
// Error
@@ -291,13 +297,13 @@ private PaginatedResponse<O> executePage(@Nullable Map<String, Object> queryPara
291297
if (page != null) params.put("page", page);
292298
if (limit != null) params.put("limit", limit);
293299

294-
final String endpointPath = getPath();
300+
final String url = buildUrl(getPath(), params);
295301
final String objectField = getPaginatedFieldName();
296302

297303
HttpURLConnection connection = null;
298304
try {
299305
// Open connection and get details
300-
final ConnectionDetails details = openConnection(endpointPath, objectField, params);
306+
final ConnectionDetails details = openConnection(url, objectField);
301307
connection = details.connection;
302308

303309
// Parse objects
@@ -313,21 +319,21 @@ private PaginatedResponse<O> executePage(@Nullable Map<String, Object> queryPara
313319
} catch (final RuntimeException e) {
314320
throw e;
315321
} catch (final Exception e) {
316-
throw new EAHttpRequestException("GET " + endpointPath, e);
322+
throw new EAHttpRequestException("GET " + url, e);
317323
} finally {
318324
if (connection != null) connection.disconnect();
319325
}
320326
}
321327

322328
@NotNull
323329
private EAItemData<O> executeOne(@Nullable String... pathSegments) {
324-
final String endpointPath = getPath();
330+
final String url = buildUrl(getPath(), null, pathSegments);
325331
final String objectField = getSingleFieldName();
326332

327333
HttpURLConnection connection = null;
328334
try {
329335
// Open connection and get details
330-
final ConnectionDetails details = openConnection(endpointPath, objectField, null, pathSegments);
336+
final ConnectionDetails details = openConnection(url, objectField);
331337
connection = details.connection;
332338

333339
// Parse object
@@ -339,7 +345,7 @@ private EAItemData<O> executeOne(@Nullable String... pathSegments) {
339345
} catch (final RuntimeException e) {
340346
throw e;
341347
} catch (final Exception e) {
342-
throw new EAHttpRequestException("GET " + endpointPath, e);
348+
throw new EAHttpRequestException("GET " + url, e);
343349
} finally {
344350
if (connection != null) connection.disconnect();
345351
}

http/src/test/java/gg/eventalerts/http/endpoint/EAEndpointRetrieveTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import gg.eventalerts.sdk.object.EAEvent;
1111
import org.bson.types.ObjectId;
1212
import org.jetbrains.annotations.NotNull;
13-
import org.jetbrains.annotations.Nullable;
1413
import org.junit.jupiter.api.BeforeEach;
1514
import org.junit.jupiter.api.AfterAll;
1615
import org.junit.jupiter.api.BeforeAll;
@@ -106,7 +105,7 @@ void retrieveOneOnErrorMapSeesTransportFailure() {
106105
final EAHTTP http = new EAHTTP.Builder("EventAlertsSDK/1.0").url(baseUrl).build();
107106
final EAEvents events = new EAEvents(http) {
108107
@Override @NotNull
109-
protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNull String objectField, @Nullable Map<String, Object> queryParams, @Nullable String... pathSegments) throws IOException {
108+
protected ConnectionDetails openConnection(@NotNull String url, @NotNull String objectField) throws IOException {
110109
throw new IOException("boom");
111110
}
112111
};
@@ -125,7 +124,7 @@ protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNul
125124
assertNotNull(event);
126125
assertEquals("Recovered Event", event.title);
127126
assertInstanceOf(EAHttpRequestException.class, captured.get());
128-
assertTrue(captured.get().getMessage().contains("GET events"));
127+
assertTrue(captured.get().getMessage().contains("GET " + events.buildUrl(events.getPath(), null, "broken")));
129128
}
130129

131130
@Test
@@ -151,7 +150,7 @@ void retrieveDoesNotSwallowFatalErrors() {
151150
final EAHTTP http = new EAHTTP.Builder("EventAlertsSDK/1.0").url(baseUrl).build();
152151
final EAEvents events = new EAEvents(http) {
153152
@Override @NotNull
154-
protected ConnectionDetails openConnection(@NotNull String endpointPath, @NotNull String objectField, @Nullable Map<String, Object> queryParams, @Nullable String... pathSegments) {
153+
protected ConnectionDetails openConnection(@NotNull String url, @NotNull String objectField) {
155154
throw new AssertionError("fatal");
156155
}
157156
};

0 commit comments

Comments
 (0)