Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't return dev messages in getUserMessage() #1940

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 deletions src/main/java/com/stripe/exception/StripeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,24 @@ protected StripeException(
}

/**
* Returns a description of the exception, including the HTTP status code and request ID (if
* applicable).
* Returns a description of the issue suitable to show an end-user (if available).
*
* @return a string representation of the user facing exception (or `null`).
*/
public String getUserMessage() {
if (this.stripeError == null) {
return null;
}
// pulls the `user_message` field, which _may_ be present in v2 errors
return this.stripeError.getUserMessage();
}

/**
* Returns a developer-facing description of the exception, including the HTTP status code and request ID (if applicable). Not suitable for showing to end users.
*
* @return a string representation of the exception.
*/
@Override
public String getMessage() {
public String getDebugMessage() {
String additionalInfo = "";
if (code != null) {
additionalInfo += "; code: " + code;
Expand All @@ -79,29 +90,13 @@ 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
*
* @return a string representation of the user facing exception.
*/
public String getUserMessage() {
if (this.getStripeError() != null) {
switch (stripeErrorApiMode) {
case V1:
return this.getStripeError().getMessage();
case V2:
return this.getStripeError().getUserMessage();
}
}
return null;
}

public static StripeException parseV2Exception(
String type,
JsonObject body,
Expand Down
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
Loading