Skip to content

Commit

Permalink
Add back IdempotencyException and add a test (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
helenye-stripe authored Sep 28, 2024
1 parent 5cd2f1b commit dc082a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/stripe/exception/IdempotencyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.stripe.exception;

public class IdempotencyException extends StripeException {
private static final long serialVersionUID = 2L;

public IdempotencyException(String message, String requestId, String code, Integer statusCode) {
super(message, requestId, code, statusCode);
}
}
8 changes: 2 additions & 6 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,8 @@ private void handleV1ApiError(StripeResponse response) throws StripeException {
case 404:
if ("idempotency_error".equals(error.getType())) {
exception =
StripeException.parseV2Exception(
"idempotency_error",
ApiResource.GSON.fromJson(response.body(), JsonObject.class),
response.code(),
response.requestId(),
this);
new IdempotencyException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
} else {
exception =
new InvalidRequestException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.JsonSyntaxException;
import com.stripe.BaseStripeTest;
import com.stripe.exception.ApiException;
import com.stripe.exception.IdempotencyException;
import com.stripe.exception.StripeException;
import com.stripe.model.Subscription;
import com.stripe.net.ApiResource;
Expand Down Expand Up @@ -42,4 +43,24 @@ public void testInvalidJson() throws StripeException {
assertNotNull(exception.getCause());
assertThat(exception.getCause(), CoreMatchers.instanceOf(JsonSyntaxException.class));
}

@Test
public void testIdempotencyError() throws StripeException {
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
ApiResource.setGlobalResponseGetter(srg);
StripeResponse response =
new StripeResponse(
400,
HttpHeaders.of(Collections.emptyMap()),
"{\"error\": {\"message\": \"idempotency\", \"type\": \"idempotency_error\"}}");
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());
Exception exception =
assertThrows(
IdempotencyException.class,
() -> {
Subscription.retrieve("sub_123");
});
assertThat(exception.getMessage(), CoreMatchers.containsString("idempotency"));
}
}

0 comments on commit dc082a2

Please sign in to comment.