Skip to content

Commit 1f7295b

Browse files
authored
Fix StripeException#getUserMessage on v1 API errors (#1911)
added stripeErrorApiMode field to StripeException to let us capture the API version that produced the error when setStripeError or setStripeV2Error is called updated getUserMessage to return stripeError.getMessage for v1 errors added StripeExceptionTest tests
1 parent 180acd8 commit 1f7295b

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.JsonObject;
44
import com.stripe.model.StripeError;
5+
import com.stripe.net.ApiMode;
56
import com.stripe.net.StripeResponseGetter;
67
import lombok.Getter;
78

@@ -14,10 +15,19 @@ public abstract class StripeException extends Exception {
1415
// implement Serializable
1516
transient StripeError stripeError;
1617

18+
// This field and its getter are used internally and may change in a non-major version
19+
// of the SDK
20+
ApiMode stripeErrorApiMode;
21+
1722
public void setStripeError(StripeError err) {
1823
stripeError = err;
24+
stripeErrorApiMode = ApiMode.V1;
1925
}
2026

27+
public void setStripeV2Error(StripeError err) {
28+
stripeError = err;
29+
stripeErrorApiMode = ApiMode.V2;
30+
}
2131
/**
2232
* Returns the error code of the response that triggered this exception. For {@link ApiException}
2333
* the error code will be equal to {@link StripeError#getCode()}.
@@ -68,7 +78,8 @@ public String getMessage() {
6878
if (requestId != null) {
6979
additionalInfo += "; request-id: " + requestId;
7080
}
71-
if (this.getUserMessage() != null) {
81+
// a separate user message is only available on v2 errors
82+
if (stripeErrorApiMode == ApiMode.V2 && this.getUserMessage() != null) {
7283
additionalInfo += "; user-message: " + this.getUserMessage();
7384
}
7485
return super.getMessage() + additionalInfo;
@@ -81,7 +92,12 @@ public String getMessage() {
8192
*/
8293
public String getUserMessage() {
8394
if (this.getStripeError() != null) {
84-
return this.getStripeError().getUserMessage();
95+
switch (stripeErrorApiMode) {
96+
case V1:
97+
return this.getStripeError().getMessage();
98+
case V2:
99+
return this.getStripeError().getUserMessage();
100+
}
85101
}
86102
return null;
87103
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ private void handleV2ApiError(StripeResponse response) throws StripeException {
409409
error.setLastResponse(response);
410410
exception =
411411
new ApiException(error.getMessage(), response.requestId(), code, response.code(), null);
412-
exception.setStripeError(error);
412+
exception.setStripeV2Error(error);
413413
throw exception;
414414
}
415415

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.stripe.exception;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import com.google.gson.JsonObject;
7+
import com.stripe.BaseStripeTest;
8+
import com.stripe.model.StripeError;
9+
import com.stripe.net.ApiMode;
10+
import com.stripe.net.ApiResource;
11+
import java.io.IOException;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class StripeExceptionTest extends BaseStripeTest {
15+
16+
@Test
17+
public void testSetStripeError() throws IOException {
18+
final String data = getResourceAsString("/api_fixtures/error_invalid_request.json");
19+
final JsonObject jsonObject =
20+
ApiResource.GSON.fromJson(data, JsonObject.class).getAsJsonObject("error");
21+
final StripeError error = ApiResource.GSON.fromJson(jsonObject, StripeError.class);
22+
error.setUserMessage("it broke");
23+
24+
StripeException exception =
25+
new StripeException(error.getMessage(), "1234", error.getCode(), 400) {};
26+
27+
exception.setStripeError(error);
28+
29+
assertNotNull(exception.getStripeError());
30+
assertEquals(ApiMode.V1, exception.getStripeErrorApiMode());
31+
32+
assertEquals("parameter_unknown", exception.getCode());
33+
assertEquals(
34+
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234",
35+
exception.getMessage());
36+
assertEquals(error.getMessage(), exception.getUserMessage());
37+
}
38+
39+
@Test
40+
public void testSetStripeV2Error() throws IOException {
41+
final String data = getResourceAsString("/api_fixtures/error_invalid_request.json");
42+
final JsonObject jsonObject =
43+
ApiResource.GSON.fromJson(data, JsonObject.class).getAsJsonObject("error");
44+
final StripeError error = ApiResource.GSON.fromJson(jsonObject, StripeError.class);
45+
StripeException exception =
46+
new StripeException(error.getMessage(), "1234", error.getCode(), 400) {};
47+
error.setUserMessage("it broke");
48+
49+
exception.setStripeV2Error(error);
50+
51+
assertNotNull(exception.getStripeError());
52+
assertEquals(ApiMode.V2, exception.getStripeErrorApiMode());
53+
54+
assertNotNull(error);
55+
assertEquals("parameter_unknown", exception.getCode());
56+
assertEquals(
57+
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234; user-message: it broke",
58+
exception.getMessage());
59+
assertEquals("it broke", exception.getUserMessage());
60+
}
61+
}

0 commit comments

Comments
 (0)