Skip to content

Commit 0f94acf

Browse files
Catch JsonSyntaxException when processing all errors (#1919)
* Catch JsonSyntaxExceptions when processing errors as well * spotless
1 parent 48d4fb9 commit 0f94acf

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/main/java/com/stripe/net/LiveStripeResponseGetter.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.google.gson.JsonSyntaxException;
77
import com.stripe.Stripe;
88
import com.stripe.exception.*;
9-
import com.stripe.exception.ApiKeyMissingException;
109
import com.stripe.exception.oauth.InvalidClientException;
1110
import com.stripe.exception.oauth.InvalidGrantException;
1211
import com.stripe.exception.oauth.InvalidScopeException;
@@ -289,22 +288,27 @@ private StripeError parseStripeError(
289288
}
290289

291290
private void handleError(StripeResponse response, ApiMode apiMode) throws StripeException {
292-
JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class);
293-
294-
/*
295-
OAuth errors are JSON objects where `error` is a string. In
296-
contrast, in API errors, `error` is a hash with sub-keys. We use
297-
this property to distinguish between OAuth and API errors.
298-
*/
299-
if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {
300-
JsonPrimitive error = responseBody.getAsJsonPrimitive("error");
301-
if (error.isString()) {
302-
handleOAuthError(response);
291+
try {
292+
/*
293+
OAuth errors are JSON objects where `error` is a string. In
294+
contrast, in API errors, `error` is a hash with sub-keys. We use
295+
this property to distinguish between OAuth and API errors.
296+
297+
Try to read the response body to see if it must be
298+
*/
299+
JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class);
300+
if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {
301+
JsonPrimitive error = responseBody.getAsJsonPrimitive("error");
302+
if (error.isString()) {
303+
handleOAuthError(response);
304+
}
305+
} else if (apiMode == ApiMode.V2) {
306+
handleV2ApiError(response);
307+
} else {
308+
handleV1ApiError(response);
303309
}
304-
} else if (apiMode == ApiMode.V2) {
305-
handleV2ApiError(response);
306-
} else {
307-
handleV1ApiError(response);
310+
} catch (JsonSyntaxException e) {
311+
throw makeMalformedJsonError(response.body(), response.code(), response.requestId(), e);
308312
}
309313
}
310314

src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,19 @@ public void testIdempotencyError() throws StripeException {
6363
});
6464
assertThat(exception.getMessage(), CoreMatchers.containsString("idempotency"));
6565
}
66+
67+
@Test
68+
public void testErrorWithJsonSyntaxException() throws Exception {
69+
HttpClient spy = Mockito.spy(new HttpURLConnectionClient());
70+
StripeResponseGetter srg = new LiveStripeResponseGetter(spy);
71+
ApiResource.setGlobalResponseGetter(srg);
72+
StripeResponse response =
73+
new StripeResponse(400, HttpHeaders.of(Collections.emptyMap()), "I am not JSON :)");
74+
Mockito.doReturn(response).when(spy).requestWithRetries(Mockito.<StripeRequest>any());
75+
assertThrows(
76+
StripeException.class,
77+
() -> {
78+
Subscription.retrieve("sub_123");
79+
});
80+
}
6681
}

0 commit comments

Comments
 (0)