@@ -331,110 +331,69 @@ private void handleError(StripeResponse response, ApiMode apiMode) throws Stripe
331331 }
332332
333333 private void handleV1ApiError (StripeResponse response ) throws StripeException {
334- StripeException exception = null ;
335-
336- StripeError error =
337- parseStripeError (response .body (), response .code (), response .requestId (), StripeError .class );
338-
339- error .setLastResponse (response );
340- switch (response .code ()) {
341- case 400 :
342- case 404 :
343- if ("idempotency_error" .equals (error .getType ())) {
344- exception =
345- new IdempotencyException (
346- error .getMessage (), response .requestId (), error .getCode (), response .code ());
347- } else {
348- exception =
349- new InvalidRequestException (
350- error .getMessage (),
351- error .getParam (),
352- response .requestId (),
353- error .getCode (),
354- response .code (),
355- null );
356- }
357- break ;
358- case 401 :
359- exception =
360- new AuthenticationException (
361- error .getMessage (), response .requestId (), error .getCode (), response .code ());
362- break ;
363- case 402 :
364- exception =
365- new CardException (
366- error .getMessage (),
367- response .requestId (),
368- error .getCode (),
369- error .getParam (),
370- error .getDeclineCode (),
371- error .getCharge (),
372- response .code (),
373- null );
374- break ;
375- case 403 :
376- exception =
377- new PermissionException (
378- error .getMessage (), response .requestId (), error .getCode (), response .code ());
379- break ;
380- case 429 :
381- exception =
382- new RateLimitException (
383- error .getMessage (),
384- error .getParam (),
385- response .requestId (),
386- error .getCode (),
387- response .code (),
388- null );
389- break ;
390- default :
391- exception =
392- new ApiException (
393- error .getMessage (), response .requestId (), error .getCode (), response .code (), null );
394- break ;
395- }
396- exception .setStripeError (error );
397-
398- throw exception ;
334+ throwStripeException (response , ApiMode .V1 );
399335 }
400336
401337 private void handleV2ApiError (StripeResponse response ) throws StripeException {
338+ // First try to throw an exception based on the "type" field, if it exists and we
339+ // recognize it. Otherwise, we will fall back to throwing an exception based on status code.
402340 JsonObject body =
403341 ApiResource .GSON .fromJson (response .body (), JsonObject .class ).getAsJsonObject ("error" );
404-
405342 JsonElement typeElement = body == null ? null : body .get ("type" );
406- JsonElement codeElement = body == null ? null : body .get ("code" );
407343 String type = typeElement == null ? "<no_type>" : typeElement .getAsString ();
408- String code = codeElement == null ? "<no_code>" : codeElement .getAsString ();
409-
410344 StripeException exception =
411345 StripeException .parseV2Exception (type , body , response .code (), response .requestId (), this );
412346 if (exception != null ) {
413347 throw exception ;
414348 }
415349
416- StripeError error ;
417- try {
418- error =
419- parseStripeError (
420- response .body (), response .code (), response .requestId (), StripeError .class );
421- } catch (ApiException e ) {
422- String message = "Unrecognized error type '" + type + "'" ;
423- JsonElement messageField = body == null ? null : body .get ("message" );
424- if (messageField != null && messageField .isJsonPrimitive ()) {
425- message = messageField .getAsString ();
426- }
427-
428- throw new ApiException (message , response .requestId (), code , response .code (), null );
429- }
350+ throwStripeException (response , ApiMode .V2 );
351+ }
430352
353+ private void throwStripeException (StripeResponse response , ApiMode apiMode )
354+ throws StripeException {
355+ StripeError error =
356+ parseStripeError (response .body (), response .code (), response .requestId (), StripeError .class );
431357 error .setLastResponse (response );
432- exception =
433- new ApiException (error .getMessage (), response .requestId (), code , response .code (), null );
434- exception .setStripeV2Error (error );
358+ StripeException exception = exceptionFromStatus (response .code (), response .requestId (), error );
359+ exception .setStripeError (error , apiMode );
435360 throw exception ;
436361 }
437362
363+ private StripeException exceptionFromStatus (int statusCode , String requestId , StripeError error ) {
364+ switch (statusCode ) {
365+ case 400 :
366+ case 404 :
367+ if ("idempotency_error" .equals (error .getType ())) {
368+ return new IdempotencyException (
369+ error .getMessage (), requestId , error .getCode (), statusCode );
370+ } else {
371+ return new InvalidRequestException (
372+ error .getMessage (), error .getParam (), requestId , error .getCode (), statusCode , null );
373+ }
374+ case 401 :
375+ return new AuthenticationException (
376+ error .getMessage (), requestId , error .getCode (), statusCode );
377+ case 402 :
378+ return new CardException (
379+ error .getMessage (),
380+ requestId ,
381+ error .getCode (),
382+ error .getParam (),
383+ error .getDeclineCode (),
384+ error .getCharge (),
385+ statusCode ,
386+ null );
387+ case 403 :
388+ return new PermissionException (error .getMessage (), requestId , error .getCode (), statusCode );
389+ case 429 :
390+ return new RateLimitException (
391+ error .getMessage (), error .getParam (), requestId , error .getCode (), statusCode , null );
392+ default :
393+ return new ApiException (error .getMessage (), requestId , error .getCode (), statusCode , null );
394+ }
395+ }
396+
438397 private void handleOAuthError (StripeResponse response ) throws StripeException {
439398 OAuthError error = null ;
440399 StripeException exception = null ;
0 commit comments