Skip to content

Commit

Permalink
don't return dev messages in getUserMessage()
Browse files Browse the repository at this point in the history
  • Loading branch information
xavdid-stripe committed Jan 23, 2025
1 parent d9d82d3 commit 2db7b1b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/main/java/com/stripe/exception/StripeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ protected StripeException(
}

/**
* Returns a description of the exception, including the HTTP status code and request ID (if
* applicable).
* Returns a developer-facing description of the exception, including the HTTP status code and
* request ID (if applicable).
*
* @return a string representation of the exception.
*/
Expand All @@ -79,25 +79,22 @@ public String getMessage() {
additionalInfo += "; request-id: " + requestId;
}
// a separate user message is only available on v2 errors
if (stripeErrorApiMode == ApiMode.V2 && this.getUserMessage() != null) {
additionalInfo += "; user-message: " + this.getUserMessage();
String userMessage = this.getUserMessage();
if (userMessage != null) {
additionalInfo += "; user-message: " + userMessage;
}
return super.getMessage() + additionalInfo;
}

/**
* Returns a description of the user facing exception
* Returns a description of the issue suitable to show an end-user (if available).
*
* @return a string representation of the user facing exception.
* @return a string representation of the user facing exception (or null).
*/
public String getUserMessage() {
if (this.getStripeError() != null) {
switch (stripeErrorApiMode) {
case V1:
return this.getStripeError().getMessage();
case V2:
return this.getStripeError().getUserMessage();
}
// only V2 errors have (optional) user messages
if (this.getStripeError() != null && stripeErrorApiMode == ApiMode.V2) {
return this.getStripeError().getUserMessage();
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/stripe/exception/StripeExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void testSetStripeError() throws IOException {
assertEquals(
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234",
exception.getMessage());
assertEquals(error.getMessage(), exception.getUserMessage());
// v1 errors don't have user messages
assertEquals(null, exception.getUserMessage());
}

@Test
Expand Down

0 comments on commit 2db7b1b

Please sign in to comment.