|
6 | 6 | import com.google.gson.JsonSyntaxException;
|
7 | 7 | import com.stripe.Stripe;
|
8 | 8 | import com.stripe.exception.*;
|
9 |
| -import com.stripe.exception.ApiKeyMissingException; |
10 | 9 | import com.stripe.exception.oauth.InvalidClientException;
|
11 | 10 | import com.stripe.exception.oauth.InvalidGrantException;
|
12 | 11 | import com.stripe.exception.oauth.InvalidScopeException;
|
@@ -289,22 +288,27 @@ private StripeError parseStripeError(
|
289 | 288 | }
|
290 | 289 |
|
291 | 290 | 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); |
303 | 309 | }
|
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); |
308 | 312 | }
|
309 | 313 | }
|
310 | 314 |
|
|
0 commit comments