Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fire response hook before parsing API error to avoid early exit #323

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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

- Fix bug where response hooks were not being called if an API request failed

## v7.4.0 (2024-07-24)

- Add new `Claim` service for filing claims on EasyPost shipments and insurances
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/easypost/http/Requestor.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,15 @@ private static <T> T httpRequest(final RequestMethod method, final String url, f
}
int rCode = response.getResponseCode();
String rBody = response.getResponseBody();
if (rCode < HttpURLConnection.HTTP_OK || rCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
handleAPIError(rBody, rCode);
}

ResponseHookResponses responseHookResponses = new ResponseHookResponses(rCode, headers, method.toString(), url,
rBody, Instant.now().toString(), requestTimestamp.toString(), requestUuid.toString());

client.getResponseHooks().executeEventHandler(responseHookResponses);

if (rCode < HttpURLConnection.HTTP_OK || rCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
handleAPIError(rBody, rCode);
}

return Constants.Http.GSON.fromJson(rBody, clazz);
}

Expand Down
88 changes: 88 additions & 0 deletions src/test/cassettes/hook/http_error.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/test/java/com/easypost/CarrierAccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ public void testCreateWithCustomWorkflow() throws EasyPostException {
public void testCreateWithUPS() throws EasyPostException {
vcr.setUpTest("create_with_ups");

Map<String, Object> data = new HashMap<>();
data.put("type", "UpsAccount");
data.put("account_number", "123456789");

CarrierAccount upsAccount = createUpsCarrierAccount();

assertInstanceOf(CarrierAccount.class, upsAccount);
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/easypost/HookTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.easypost;

import com.easypost.exception.API.NotFoundError;
import com.easypost.exception.EasyPostException;
import com.easypost.hooks.RequestHookResponses;
import com.easypost.hooks.ResponseHookResponses;
Expand All @@ -9,13 +10,16 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.function.Function;

public class HookTest {
private static TestUtils.VCR vcr;

private static boolean hookHit = false;

/**
* Set up the testing environment for this file.
*
Expand Down Expand Up @@ -67,6 +71,27 @@ public static Object testRequestHooks(RequestHookResponses data) {
return true;
}

/**
* Test subscribing a response hook when an HTTP error occurs.
*
* @param data The ResponseHookResponses object representing the hook data.
* @return The result of the test.
*/
public static Object testResponseHookOnHttpError(ResponseHookResponses data) {
assertEquals("https://api.easypost.com/v2/parcels/par_123", data.getPath());
assertEquals("GET", data.getMethod());
assertEquals(404, data.getHttpStatus());
assertNotNull(data.getHeaders());
assertNotNull(data.getResponseBody());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestTimestamp());
assertNotNull(data.getRequestUuid());

hookHit = true;

return true;
}

/**
* Test subscribing a response hook.
*
Expand Down Expand Up @@ -132,6 +157,29 @@ public void testUnsubscribeHooks() throws EasyPostException {
vcr.client.unsubscribeFromResponseHook(failedResponseHook);

vcr.client.parcel.create(Fixtures.basicParcel());
}

/**
* Test that response hooks are still fired even if the HTTP call fails.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testResponseHookFiredOnHTTPError() throws EasyPostException {
vcr.setUpTest("http_error");

hookHit = false;

Function<ResponseHookResponses, Object> requestHook = HookTest::testResponseHookOnHttpError;

vcr.client.subscribeToResponseHook(requestHook);

try {
vcr.client.parcel.retrieve("par_123");
} catch (NotFoundError e) {
assertEquals(404, e.getStatusCode());
}

assertTrue(hookHit);
}
}
Loading