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

Fix StripeException#getUserMessage on v1 API errors #1911

Merged
merged 2 commits into from
Oct 25, 2024
Merged
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
20 changes: 18 additions & 2 deletions src/main/java/com/stripe/exception/StripeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonObject;
import com.stripe.model.StripeError;
import com.stripe.net.ApiMode;
import com.stripe.net.StripeResponseGetter;
import lombok.Getter;

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

// This field and its getter are used internally and may change in a non-major version
// of the SDK
ApiMode stripeErrorApiMode;

public void setStripeError(StripeError err) {
stripeError = err;
stripeErrorApiMode = ApiMode.V1;
}

public void setStripeV2Error(StripeError err) {
stripeError = err;
stripeErrorApiMode = ApiMode.V2;
}
/**
* Returns the error code of the response that triggered this exception. For {@link ApiException}
* the error code will be equal to {@link StripeError#getCode()}.
Expand Down Expand Up @@ -68,7 +78,8 @@ public String getMessage() {
if (requestId != null) {
additionalInfo += "; request-id: " + requestId;
}
if (this.getUserMessage() != null) {
// a separate user message is only available on v2 errors
if (stripeErrorApiMode == ApiMode.V2 && this.getUserMessage() != null) {
additionalInfo += "; user-message: " + this.getUserMessage();
}
return super.getMessage() + additionalInfo;
Expand All @@ -81,7 +92,12 @@ public String getMessage() {
*/
public String getUserMessage() {
if (this.getStripeError() != null) {
return this.getStripeError().getUserMessage();
switch (stripeErrorApiMode) {
case V1:
return this.getStripeError().getMessage();
case V2:
return this.getStripeError().getUserMessage();
}
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private void handleV2ApiError(StripeResponse response) throws StripeException {
error.setLastResponse(response);
exception =
new ApiException(error.getMessage(), response.requestId(), code, response.code(), null);
exception.setStripeError(error);
exception.setStripeV2Error(error);
throw exception;
}

Expand Down
61 changes: 61 additions & 0 deletions src/test/java/com/stripe/exception/StripeExceptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.stripe.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.google.gson.JsonObject;
import com.stripe.BaseStripeTest;
import com.stripe.model.StripeError;
import com.stripe.net.ApiMode;
import com.stripe.net.ApiResource;
import java.io.IOException;
import org.junit.jupiter.api.Test;

public class StripeExceptionTest extends BaseStripeTest {

@Test
public void testSetStripeError() throws IOException {
final String data = getResourceAsString("/api_fixtures/error_invalid_request.json");
final JsonObject jsonObject =
ApiResource.GSON.fromJson(data, JsonObject.class).getAsJsonObject("error");
final StripeError error = ApiResource.GSON.fromJson(jsonObject, StripeError.class);
error.setUserMessage("it broke");

StripeException exception =
new StripeException(error.getMessage(), "1234", error.getCode(), 400) {};

exception.setStripeError(error);

assertNotNull(exception.getStripeError());
assertEquals(ApiMode.V1, exception.getStripeErrorApiMode());

assertEquals("parameter_unknown", exception.getCode());
assertEquals(
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234",
exception.getMessage());
assertEquals(error.getMessage(), exception.getUserMessage());
}

@Test
public void testSetStripeV2Error() throws IOException {
final String data = getResourceAsString("/api_fixtures/error_invalid_request.json");
final JsonObject jsonObject =
ApiResource.GSON.fromJson(data, JsonObject.class).getAsJsonObject("error");
final StripeError error = ApiResource.GSON.fromJson(jsonObject, StripeError.class);
StripeException exception =
new StripeException(error.getMessage(), "1234", error.getCode(), 400) {};
error.setUserMessage("it broke");

exception.setStripeV2Error(error);

assertNotNull(exception.getStripeError());
assertEquals(ApiMode.V2, exception.getStripeErrorApiMode());

assertNotNull(error);
assertEquals("parameter_unknown", exception.getCode());
assertEquals(
"Received unknown parameter: foo; code: parameter_unknown; request-id: 1234; user-message: it broke",
exception.getMessage());
assertEquals("it broke", exception.getUserMessage());
}
}
Loading