Skip to content

Commit c1e7f1b

Browse files
⚠️ Use HTTP status code in V2 errors (#2131)
* Use HTTP status code in V2 errors * Fix formatting
1 parent a2bc4be commit c1e7f1b

File tree

4 files changed

+420
-96
lines changed

4 files changed

+420
-96
lines changed

src/main/java/com/stripe/exception/RateLimitException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import lombok.Getter;
88

99
@Getter
10-
public class RateLimitException extends ApiException {
10+
public class RateLimitException extends StripeException {
1111
private static final long serialVersionUID = 2L;
1212

1313
private final String param;

src/main/java/com/stripe/exception/StripeException.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ public abstract class StripeException extends Exception {
2020
ApiMode stripeErrorApiMode;
2121

2222
public void setStripeError(StripeError err) {
23+
setStripeError(err, ApiMode.V1);
24+
}
25+
26+
public void setStripeError(StripeError err, ApiMode mode) {
2327
stripeError = err;
24-
stripeErrorApiMode = ApiMode.V1;
28+
stripeErrorApiMode = mode;
2529
}
2630

2731
public void setStripeV2Error(StripeError err) {
28-
stripeError = err;
29-
stripeErrorApiMode = ApiMode.V2;
32+
setStripeError(err, ApiMode.V2);
3033
}
3134
/**
3235
* Returns the error code of the response that triggered this exception. For {@link ApiException}

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

Lines changed: 45 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -311,110 +311,69 @@ private void handleError(StripeResponse response, ApiMode apiMode) throws Stripe
311311
}
312312

313313
private void handleV1ApiError(StripeResponse response) throws StripeException {
314-
StripeException exception = null;
315-
316-
StripeError error =
317-
parseStripeError(response.body(), response.code(), response.requestId(), StripeError.class);
318-
319-
error.setLastResponse(response);
320-
switch (response.code()) {
321-
case 400:
322-
case 404:
323-
if ("idempotency_error".equals(error.getType())) {
324-
exception =
325-
new IdempotencyException(
326-
error.getMessage(), response.requestId(), error.getCode(), response.code());
327-
} else {
328-
exception =
329-
new InvalidRequestException(
330-
error.getMessage(),
331-
error.getParam(),
332-
response.requestId(),
333-
error.getCode(),
334-
response.code(),
335-
null);
336-
}
337-
break;
338-
case 401:
339-
exception =
340-
new AuthenticationException(
341-
error.getMessage(), response.requestId(), error.getCode(), response.code());
342-
break;
343-
case 402:
344-
exception =
345-
new CardException(
346-
error.getMessage(),
347-
response.requestId(),
348-
error.getCode(),
349-
error.getParam(),
350-
error.getDeclineCode(),
351-
error.getCharge(),
352-
response.code(),
353-
null);
354-
break;
355-
case 403:
356-
exception =
357-
new PermissionException(
358-
error.getMessage(), response.requestId(), error.getCode(), response.code());
359-
break;
360-
case 429:
361-
exception =
362-
new RateLimitException(
363-
error.getMessage(),
364-
error.getParam(),
365-
response.requestId(),
366-
error.getCode(),
367-
response.code(),
368-
null);
369-
break;
370-
default:
371-
exception =
372-
new ApiException(
373-
error.getMessage(), response.requestId(), error.getCode(), response.code(), null);
374-
break;
375-
}
376-
exception.setStripeError(error);
377-
378-
throw exception;
314+
throwStripeException(response, ApiMode.V1);
379315
}
380316

381317
private void handleV2ApiError(StripeResponse response) throws StripeException {
318+
// First try to throw an exception based on the "type" field, if it exists and we
319+
// recognize it. Otherwise, we will fall back to throwing an exception based on status code.
382320
JsonObject body =
383321
ApiResource.GSON.fromJson(response.body(), JsonObject.class).getAsJsonObject("error");
384-
385322
JsonElement typeElement = body == null ? null : body.get("type");
386-
JsonElement codeElement = body == null ? null : body.get("code");
387323
String type = typeElement == null ? "<no_type>" : typeElement.getAsString();
388-
String code = codeElement == null ? "<no_code>" : codeElement.getAsString();
389-
390324
StripeException exception =
391325
StripeException.parseV2Exception(type, body, response.code(), response.requestId(), this);
392326
if (exception != null) {
393327
throw exception;
394328
}
395329

396-
StripeError error;
397-
try {
398-
error =
399-
parseStripeError(
400-
response.body(), response.code(), response.requestId(), StripeError.class);
401-
} catch (ApiException e) {
402-
String message = "Unrecognized error type '" + type + "'";
403-
JsonElement messageField = body == null ? null : body.get("message");
404-
if (messageField != null && messageField.isJsonPrimitive()) {
405-
message = messageField.getAsString();
406-
}
407-
408-
throw new ApiException(message, response.requestId(), code, response.code(), null);
409-
}
330+
throwStripeException(response, ApiMode.V2);
331+
}
410332

333+
private void throwStripeException(StripeResponse response, ApiMode apiMode)
334+
throws StripeException {
335+
StripeError error =
336+
parseStripeError(response.body(), response.code(), response.requestId(), StripeError.class);
411337
error.setLastResponse(response);
412-
exception =
413-
new ApiException(error.getMessage(), response.requestId(), code, response.code(), null);
414-
exception.setStripeV2Error(error);
338+
StripeException exception = exceptionFromStatus(response.code(), response.requestId(), error);
339+
exception.setStripeError(error, apiMode);
415340
throw exception;
416341
}
417342

343+
private StripeException exceptionFromStatus(int statusCode, String requestId, StripeError error) {
344+
switch (statusCode) {
345+
case 400:
346+
case 404:
347+
if ("idempotency_error".equals(error.getType())) {
348+
return new IdempotencyException(
349+
error.getMessage(), requestId, error.getCode(), statusCode);
350+
} else {
351+
return new InvalidRequestException(
352+
error.getMessage(), error.getParam(), requestId, error.getCode(), statusCode, null);
353+
}
354+
case 401:
355+
return new AuthenticationException(
356+
error.getMessage(), requestId, error.getCode(), statusCode);
357+
case 402:
358+
return new CardException(
359+
error.getMessage(),
360+
requestId,
361+
error.getCode(),
362+
error.getParam(),
363+
error.getDeclineCode(),
364+
error.getCharge(),
365+
statusCode,
366+
null);
367+
case 403:
368+
return new PermissionException(error.getMessage(), requestId, error.getCode(), statusCode);
369+
case 429:
370+
return new RateLimitException(
371+
error.getMessage(), error.getParam(), requestId, error.getCode(), statusCode, null);
372+
default:
373+
return new ApiException(error.getMessage(), requestId, error.getCode(), statusCode, null);
374+
}
375+
}
376+
418377
private void handleOAuthError(StripeResponse response) throws StripeException {
419378
OAuthError error = null;
420379
StripeException exception = null;

0 commit comments

Comments
 (0)