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

Determine exception type by error type first #1362

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ dependencies {
testImplementation group: "com.google.guava", name: "guava", version:"31.0.1-jre"
testImplementation group: "com.squareup.okhttp3", name: "mockwebserver", version: "4.9.1"
testImplementation group: "org.mockito", name: "mockito-core", version:"4.1.0"
testImplementation group: "org.junit.jupiter", name: "junit-jupiter-api", version: "5.8.2"
testImplementation group: "org.junit.jupiter", name: "junit-jupiter", version: "5.8.2"
testRuntimeOnly group: "org.junit.jupiter", name: "junit-jupiter-engine", version: "5.8.2"
testRuntimeOnly group: "org.slf4j", name: "slf4j-api", version: "1.7.32"
}
Expand Down
113 changes: 67 additions & 46 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,61 +177,82 @@ private static void handleApiError(StripeResponse response) throws StripeExcepti

error.setLastResponse(response);

switch (response.code()) {
case 400:
case 404:
if ("idempotency_error".equals(error.getType())) {
String errorType = error.getType();
if ("invalid_request_error".equals(errorType)) {
exception =
new InvalidRequestException(
error.getMessage(),
error.getParam(),
response.requestId(),
error.getCode(),
response.code(),
null);
} else if ("idempotency_error".equals(errorType)) {
exception =
new IdempotencyException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
} else if ("card_error".equals(errorType)) {
exception =
new CardException(
error.getMessage(),
response.requestId(),
error.getCode(),
error.getParam(),
error.getDeclineCode(),
error.getCharge(),
response.code(),
null);
} else {
switch (response.code()) {
case 400:
case 404:
exception =
new IdempotencyException(
new InvalidRequestException(
error.getMessage(),
error.getParam(),
response.requestId(),
error.getCode(),
response.code(),
null);
break;
case 401:
exception =
new AuthenticationException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
} else {
break;
case 402:
exception =
new InvalidRequestException(
new CardException(
error.getMessage(),
response.requestId(),
error.getCode(),
error.getParam(),
error.getDeclineCode(),
error.getCharge(),
response.code(),
null);
break;
case 403:
exception =
new PermissionException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
break;
case 429:
exception =
new RateLimitException(
error.getMessage(),
error.getParam(),
response.requestId(),
error.getCode(),
response.code(),
null);
}
break;
case 401:
exception =
new AuthenticationException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
break;
case 402:
exception =
new CardException(
error.getMessage(),
response.requestId(),
error.getCode(),
error.getParam(),
error.getDeclineCode(),
error.getCharge(),
response.code(),
null);
break;
case 403:
exception =
new PermissionException(
error.getMessage(), response.requestId(), error.getCode(), response.code());
break;
case 429:
exception =
new RateLimitException(
error.getMessage(),
error.getParam(),
response.requestId(),
error.getCode(),
response.code(),
null);
break;
default:
exception =
new ApiException(
error.getMessage(), response.requestId(), error.getCode(), response.code(), null);
break;
break;
default:
exception =
new ApiException(
error.getMessage(), response.requestId(), error.getCode(), response.code(), null);
break;
}
}

exception.setStripeError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import com.google.gson.JsonSyntaxException;
import com.stripe.BaseStripeTest;
import com.stripe.exception.ApiException;
import com.stripe.exception.StripeException;
import com.stripe.exception.*;
import com.stripe.model.Subscription;
import com.stripe.net.ApiResource;
import com.stripe.net.HttpClient;
Expand All @@ -18,6 +17,8 @@
import java.util.Collections;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;

public class LiveStripeResponseGetterTest extends BaseStripeTest {
Expand All @@ -41,4 +42,64 @@ public void testInvalidJson() throws StripeException {
assertNotNull(exception.getCause());
assertThat(exception.getCause(), CoreMatchers.instanceOf(JsonSyntaxException.class));
}

public static Object[][] responseExceptionsByType() {
return new Object[][] {
{"card_error", CardException.class},
{"invalid_request_error", InvalidRequestException.class},
{"idempotency_error", IdempotencyException.class}
};
}

@ParameterizedTest(name = "{0}")
@MethodSource(value = "responseExceptionsByType")
public void testCreatesCorrectExceptionFromType(
String type, Class<? extends Exception> exceptionClass) throws StripeException {
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
ApiResource.setStripeResponseGetter(srg);
StripeResponse response =
new StripeResponse(
432,
HttpHeaders.of(Collections.emptyMap()),
"{\"error\":{ \"type\":\"" + type + "\" }}");
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());

assertThrows(
exceptionClass,
() -> {
Subscription.retrieve("sub_123");
});
}

public static Object[][] responseExceptionsByStatus() {
return new Object[][] {
{402, CardException.class},
{400, InvalidRequestException.class},
{404, InvalidRequestException.class},
{401, AuthenticationException.class},
{403, PermissionException.class},
{429, RateLimitException.class},
{499, ApiException.class},
};
}

@ParameterizedTest(name = "{0}")
@MethodSource(value = "responseExceptionsByStatus")
public void testCreatesCorrectExceptionFromStatus(
Integer status, Class<? extends Exception> exceptionClass) throws StripeException {
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
ApiResource.setStripeResponseGetter(srg);
StripeResponse response =
new StripeResponse(
status, HttpHeaders.of(Collections.emptyMap()), "{\"error\":{ \"type\":\"???\" }}");
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());

assertThrows(
exceptionClass,
() -> {
Subscription.retrieve("sub_123");
});
}
}