/** * Method to extract an error for an individual field, e.g. creditCard, customer, etc. * * @param field Name of the field desired, expected to be in camelCase. * @return {@link BraintreeError} for the field searched, or {@code null} if not found. */ @Nullable public BraintreeError errorFor(String field) { BraintreeError returnError; if (mFieldErrors != null) { for (BraintreeError error : mFieldErrors) { if (error.getField().equals(field)) { return error; } else if (error.getFieldErrors() != null) { returnError = error.errorFor(field); if (returnError != null) { return returnError; } } } } return null; }
/** * Method to extract an error for an individual field, e.g. creditCard, customer, etc. * * @param field Name of the field desired, expected to be in camelCase. * @return {@link BraintreeError} for the field searched, or {@code null} if not found. */ @Nullable public BraintreeError errorFor(String field) { BraintreeError returnError; if (mFieldErrors != null) { for (BraintreeError error : mFieldErrors) { if (error.getField().equals(field)) { return error; } else if (error.getFieldErrors() != null) { returnError = error.errorFor(field); if (returnError != null) { return returnError; } } } } return null; }
@Override public void onError(Exception error) { assertEquals("Postal code verification failed", ((ErrorWithResponse) error).errorFor("creditCard").errorFor("billingAddress") .getFieldErrors().get(0).getMessage()); countDownLatch.countDown(); } });
@Override public void onError(Exception error) { assertEquals("Country code (alpha3) is not an accepted country", ((ErrorWithResponse) error).errorFor("creditCard").errorFor("billingAddress") .getFieldErrors().get(0).getMessage()); countDownLatch.countDown(); } });
@Test public void fromJson_parsesErrorsCorrectly() throws JSONException { String response = stringFromFixture("errors/credit_card_error_response.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromJson(response); assertEquals("Credit card is invalid", errorWithResponse.getMessage()); assertNull(errorWithResponse.errorFor("creditCard").errorFor("postalCode")); assertEquals("Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code", errorWithResponse.errorFor("creditCard").errorFor("base").getMessage()); assertEquals("Credit card number is required", errorWithResponse.errorFor("creditCard").errorFor("number").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); }
@Test public void constructor_parsesErrorsCorrectly() { String response = stringFromFixture("errors/credit_card_error_response.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, response); assertEquals("Credit card is invalid", errorWithResponse.getMessage()); assertEquals(422, errorWithResponse.getStatusCode()); assertNull(errorWithResponse.errorFor("creditCard").errorFor("postalCode")); assertEquals("Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code", errorWithResponse.errorFor("creditCard").errorFor("base").getMessage()); assertEquals("Credit card number is required", errorWithResponse.errorFor("creditCard").errorFor("number").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); }
@Test public void fromGraphQLJson_parsesErrorsCorrectly() { String response = stringFromFixture("errors/graphql/credit_card_error.json"); ErrorWithResponse errorWithResponse = ErrorWithResponse.fromGraphQLJson(response); assertEquals("Input is invalid.", errorWithResponse.getMessage()); assertEquals(422, errorWithResponse.getStatusCode()); assertEquals("Expiration month is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationMonth").getMessage()); assertEquals("Expiration year is invalid", errorWithResponse.errorFor("creditCard").errorFor("expirationYear").getMessage()); assertEquals("CVV verification failed", errorWithResponse.errorFor("creditCard").errorFor("cvv").getMessage()); }