Skip to content

Commit 2db7b1b

Browse files
committed
don't return dev messages in getUserMessage()
1 parent d9d82d3 commit 2db7b1b

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ protected StripeException(
6464
}
6565

6666
/**
67-
* Returns a description of the exception, including the HTTP status code and request ID (if
68-
* applicable).
67+
* Returns a developer-facing description of the exception, including the HTTP status code and
68+
* request ID (if applicable).
6969
*
7070
* @return a string representation of the exception.
7171
*/
@@ -79,25 +79,22 @@ public String getMessage() {
7979
additionalInfo += "; request-id: " + requestId;
8080
}
8181
// a separate user message is only available on v2 errors
82-
if (stripeErrorApiMode == ApiMode.V2 && this.getUserMessage() != null) {
83-
additionalInfo += "; user-message: " + this.getUserMessage();
82+
String userMessage = this.getUserMessage();
83+
if (userMessage != null) {
84+
additionalInfo += "; user-message: " + userMessage;
8485
}
8586
return super.getMessage() + additionalInfo;
8687
}
8788

8889
/**
89-
* Returns a description of the user facing exception
90+
* Returns a description of the issue suitable to show an end-user (if available).
9091
*
91-
* @return a string representation of the user facing exception.
92+
* @return a string representation of the user facing exception (or null).
9293
*/
9394
public String getUserMessage() {
94-
if (this.getStripeError() != null) {
95-
switch (stripeErrorApiMode) {
96-
case V1:
97-
return this.getStripeError().getMessage();
98-
case V2:
99-
return this.getStripeError().getUserMessage();
100-
}
95+
// only V2 errors have (optional) user messages
96+
if (this.getStripeError() != null && stripeErrorApiMode == ApiMode.V2) {
97+
return this.getStripeError().getUserMessage();
10198
}
10299
return null;
103100
}

src/test/java/com/stripe/exception/StripeExceptionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void testSetStripeError() throws IOException {
3333
assertEquals(
3434
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234",
3535
exception.getMessage());
36-
assertEquals(error.getMessage(), exception.getUserMessage());
36+
// v1 errors don't have user messages
37+
assertEquals(null, exception.getUserMessage());
3738
}
3839

3940
@Test

0 commit comments

Comments
 (0)