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

Catch JsonSyntaxException when processing all errors #1919

Merged
merged 2 commits into from
Nov 6, 2024
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
36 changes: 20 additions & 16 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.google.gson.JsonSyntaxException;
import com.stripe.Stripe;
import com.stripe.exception.*;
import com.stripe.exception.ApiKeyMissingException;
import com.stripe.exception.oauth.InvalidClientException;
import com.stripe.exception.oauth.InvalidGrantException;
import com.stripe.exception.oauth.InvalidScopeException;
Expand Down Expand Up @@ -289,22 +288,27 @@ private StripeError parseStripeError(
}

private void handleError(StripeResponse response, ApiMode apiMode) throws StripeException {
JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class);

/*
OAuth errors are JSON objects where `error` is a string. In
contrast, in API errors, `error` is a hash with sub-keys. We use
this property to distinguish between OAuth and API errors.
*/
if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {
JsonPrimitive error = responseBody.getAsJsonPrimitive("error");
if (error.isString()) {
handleOAuthError(response);
try {
/*
OAuth errors are JSON objects where `error` is a string. In
contrast, in API errors, `error` is a hash with sub-keys. We use
this property to distinguish between OAuth and API errors.

Try to read the response body to see if it must be
helenye-stripe marked this conversation as resolved.
Show resolved Hide resolved
*/
JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class);
if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {
JsonPrimitive error = responseBody.getAsJsonPrimitive("error");
if (error.isString()) {
handleOAuthError(response);
}
} else if (apiMode == ApiMode.V2) {
handleV2ApiError(response);
helenye-stripe marked this conversation as resolved.
Show resolved Hide resolved
} else {
handleV1ApiError(response);
}
} else if (apiMode == ApiMode.V2) {
handleV2ApiError(response);
} else {
handleV1ApiError(response);
} catch (JsonSyntaxException e) {
helenye-stripe marked this conversation as resolved.
Show resolved Hide resolved
throw makeMalformedJsonError(response.body(), response.code(), response.requestId(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ public void testIdempotencyError() throws StripeException {
});
assertThat(exception.getMessage(), CoreMatchers.containsString("idempotency"));
}

@Test
public void testErrorWithJsonSyntaxException() throws Exception {
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
ApiResource.setGlobalResponseGetter(srg);
StripeResponse response =
new StripeResponse(400, HttpHeaders.of(Collections.emptyMap()), "I am not JSON :)");
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());
assertThrows(
StripeException.class,
() -> {
Subscription.retrieve("sub_123");
});
}
}
Loading