From 0f94acf2438d540185b93efab743a2664ebaf872 Mon Sep 17 00:00:00 2001 From: helenye-stripe <111009531+helenye-stripe@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:49:02 -0800 Subject: [PATCH 1/3] Catch `JsonSyntaxException` when processing all errors (#1919) * Catch JsonSyntaxExceptions when processing errors as well * spotless --- .../stripe/net/LiveStripeResponseGetter.java | 36 ++++++++++--------- .../LiveStripeResponseGetterTest.java | 15 ++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java index 0caba0cfd38..f11e94b87d1 100644 --- a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java +++ b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java @@ -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; @@ -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 + */ + 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); + } else { + handleV1ApiError(response); } - } else if (apiMode == ApiMode.V2) { - handleV2ApiError(response); - } else { - handleV1ApiError(response); + } catch (JsonSyntaxException e) { + throw makeMalformedJsonError(response.body(), response.code(), response.requestId(), e); } } diff --git a/src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java b/src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java index afc1f623ff4..8a470940027 100644 --- a/src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java +++ b/src/test/java/com/stripe/functional/LiveStripeResponseGetterTest.java @@ -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.any()); + assertThrows( + StripeException.class, + () -> { + Subscription.retrieve("sub_123"); + }); + } } From 85c25d793bcb6403ac80e145f8566fd7fba109d4 Mon Sep 17 00:00:00 2001 From: Helen Ye Date: Wed, 6 Nov 2024 17:00:40 -0500 Subject: [PATCH 2/3] Bump version to 28.0.1 --- CHANGELOG.md | 5 +++++ README.md | 8 ++++---- VERSION | 2 +- gradle.properties | 2 +- src/main/java/com/stripe/Stripe.java | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed2bc51c6b1..7fee7466aa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 28.0.1 - 2024-11-06 +* [#1919](https://github.com/stripe/stripe-java/pull/1919) Catch `JsonSyntaxException` when processing all errors +* [#1916](https://github.com/stripe/stripe-java/pull/1916) Restore `testReportsRawRequestUsageTelemetry` test +* [#1915](https://github.com/stripe/stripe-java/pull/1915) add major version blurb to changelog + ## 28.0.0 - 2024-10-29 Historically, when upgrading webhooks to a new API version, you also had to upgrade your SDK version. Your webhook's API version needed to match the API version pinned by the SDK you were using to ensure successful deserialization of events. With the `2024-09-30.acacia` release, Stripe follows a [new API release process](https://stripe.com/blog/introducing-stripes-new-api-release-process). As a result, you can safely upgrade your webhook endpoints to any API version within a biannual release (like `acacia`) without upgrading the SDK. diff --git a/README.md b/README.md index 65d2a34f5c3..e78b81e07fb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Stripe Java client library -[![Maven Central](https://img.shields.io/badge/maven--central-v28.0.0-blue)](https://mvnrepository.com/artifact/com.stripe/stripe-java) +[![Maven Central](https://img.shields.io/badge/maven--central-v28.0.1-blue)](https://mvnrepository.com/artifact/com.stripe/stripe-java) [![JavaDoc](http://img.shields.io/badge/javadoc-reference-blue.svg)](https://stripe.dev/stripe-java) [![Build Status](https://github.com/stripe/stripe-java/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/stripe/stripe-java/actions?query=branch%3Amaster) [![Coverage Status](https://coveralls.io/repos/github/stripe/stripe-java/badge.svg?branch=master)](https://coveralls.io/github/stripe/stripe-java?branch=master) @@ -18,7 +18,7 @@ The official [Stripe][stripe] Java client library. Add this dependency to your project's build file: ```groovy -implementation "com.stripe:stripe-java:28.0.0" +implementation "com.stripe:stripe-java:28.0.1" ``` ### Maven users @@ -29,7 +29,7 @@ Add this dependency to your project's POM: com.stripe stripe-java - 28.0.0 + 28.0.1 ``` @@ -37,7 +37,7 @@ Add this dependency to your project's POM: You'll need to manually install the following JARs: -- [The Stripe JAR](https://search.maven.org/remotecontent?filepath=com/stripe/stripe-java/28.0.0/stripe-java-28.0.0.jar) +- [The Stripe JAR](https://search.maven.org/remotecontent?filepath=com/stripe/stripe-java/28.0.1/stripe-java-28.0.1.jar) - [Google Gson][gson] from . ### [ProGuard][proguard] diff --git a/VERSION b/VERSION index 0b4dfcf095f..eebc120d380 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -28.0.0 +28.0.1 diff --git a/gradle.properties b/gradle.properties index 95d64a9796f..f63c1cf9c56 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ GROUP=com.stripe -VERSION_NAME=28.0.0 +VERSION_NAME=28.0.1 POM_URL=https://github.com/stripe/stripe-java POM_SCM_URL=git@github.com:stripe/stripe-java.git diff --git a/src/main/java/com/stripe/Stripe.java b/src/main/java/com/stripe/Stripe.java index 2e341bef4a5..9b90f76fa03 100644 --- a/src/main/java/com/stripe/Stripe.java +++ b/src/main/java/com/stripe/Stripe.java @@ -14,7 +14,7 @@ public abstract class Stripe { public static final String LIVE_API_BASE = "https://api.stripe.com"; public static final String UPLOAD_API_BASE = "https://files.stripe.com"; public static final String METER_EVENTS_API_BASE = "https://meter-events.stripe.com"; - public static final String VERSION = "28.0.0"; + public static final String VERSION = "28.0.1"; public static volatile String apiKey; public static volatile String clientId; From 27bf7cfb21b707f616546b0b9f27deb8ee19ee52 Mon Sep 17 00:00:00 2001 From: helenye-stripe <111009531+helenye-stripe@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:30:00 -0800 Subject: [PATCH 3/3] fix comment (#1920) --- src/main/java/com/stripe/net/LiveStripeResponseGetter.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java index f11e94b87d1..0df78fddf24 100644 --- a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java +++ b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java @@ -293,8 +293,6 @@ private void handleError(StripeResponse response, ApiMode apiMode) throws Stripe 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 */ JsonObject responseBody = ApiResource.GSON.fromJson(response.body(), JsonObject.class); if (responseBody.has("error") && responseBody.get("error").isJsonPrimitive()) {