-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
180acd8
commit 1f7295b
Showing
3 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/java/com/stripe/exception/StripeExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |