public ErrorWithResponse createFromParcel(Parcel source) { return new ErrorWithResponse(source); }
public static ErrorWithResponse fromJson(String json) throws JSONException { ErrorWithResponse errorWithResponse = new ErrorWithResponse(); errorWithResponse.mOriginalResponse = json; errorWithResponse.parseJson(json); return errorWithResponse; }
public static ErrorWithResponse fromGraphQLJson(String json) { ErrorWithResponse errorWithResponse = new ErrorWithResponse(); errorWithResponse.mOriginalResponse = json; errorWithResponse.mStatusCode = 422; try { JSONArray errors = new JSONObject(json).getJSONArray(Keys.ERRORS); errorWithResponse.mFieldErrors = BraintreeError.fromGraphQLJsonArray(errors); if (errorWithResponse.mFieldErrors.isEmpty()) { errorWithResponse.mMessage = errors.getJSONObject(0).getString(Keys.MESSAGE); } else { errorWithResponse.mMessage = ErrorMessages.USER; } } catch (JSONException e) { errorWithResponse.mMessage = "Parsing error response failed"; errorWithResponse.mFieldErrors = new ArrayList<>(); } return errorWithResponse; }
@Override protected String parseResponse(HttpURLConnection connection) throws Exception { try { return super.parseResponse(connection); } catch (AuthorizationException | UnprocessableEntityException e) { if (e instanceof AuthorizationException) { String errorMessage = new ErrorWithResponse(403, e.getMessage()).getMessage(); throw new AuthorizationException(errorMessage); } else { throw new ErrorWithResponse(422, e.getMessage()); } } } }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { ((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, "")); return null; } }).when(TokenizationClient.class);
@Override public Void answer(InvocationOnMock invocation) throws Throwable { ((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, "")); return null; } }).when(TokenizationClient.class);
@Test public void constructor_handlesTopLevelErrors() { String topLevelError = stringFromFixture("errors/auth_fingerprint_error.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, topLevelError); assertEquals("Authorization fingerprint is invalid", errorWithResponse.getMessage()); assertEquals(1, errorWithResponse.getFieldErrors().size()); }
@Test public void constructor_doesNotBlowUpParsingBadJson() { String badJson = stringFromFixture("random_json.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, badJson); assertEquals("Parsing error response failed", errorWithResponse.getMessage()); }
@Test public void postCallback_ErrorWithResponseIsPostedToListeners() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); fragment.addListener(new BraintreeErrorListener() { @Override public void onError(Exception error) { assertTrue(error instanceof ErrorWithResponse); assertEquals(422, ((ErrorWithResponse) error).getStatusCode()); mCalled.set(true); } }); fragment.postCallback(new ErrorWithResponse(422, "")); assertTrue(mCalled.get()); }
@Test public void addListener_flushesErrorWithResponseCallback() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); fragment.postCallback(new ErrorWithResponse(422, "")); fragment.addListener(new BraintreeErrorListener() { @Override public void onError(Exception error) { assertTrue(error instanceof ErrorWithResponse); assertEquals(422, ((ErrorWithResponse) error).getStatusCode()); mCalled.set(true); } }); assertTrue(mCalled.get()); }
@Test public void constructor_canHandleMultipleCategories() { String errors = stringFromFixture("errors/complex_error_response.json"); ErrorWithResponse errorWithResponse = new ErrorWithResponse(422, errors); assertEquals(3, errorWithResponse.errorFor("creditCard").getFieldErrors().size()); assertEquals("is invalid", errorWithResponse.errorFor("customer").getMessage()); assertEquals(0, errorWithResponse.errorFor("customer").getFieldErrors().size()); }
protected static void onActivityResult(BraintreeFragment fragment, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { ThreeDSecureAuthenticationResponse authenticationResponse; Uri resultUri = data.getData(); if (resultUri != null) { authenticationResponse = ThreeDSecureAuthenticationResponse.fromJson(resultUri.getQueryParameter("auth_response")); } else { authenticationResponse = data.getParcelableExtra(ThreeDSecureWebViewActivity.EXTRA_THREE_D_SECURE_RESULT); } if (authenticationResponse.isSuccess()) { fragment.postCallback(authenticationResponse.getCardNonce()); } else if (authenticationResponse.getException() != null) { fragment.postCallback(new BraintreeException(authenticationResponse.getException())); } else { fragment.postCallback(new ErrorWithResponse(422, authenticationResponse.getErrors())); } } }
@Test public void getErrors_returnsErrorString() { ThreeDSecureAuthenticationResponse authResponse = ThreeDSecureAuthenticationResponse.fromJson( stringFromFixture("three_d_secure/authentication_response_with_error.json")); ErrorWithResponse errors = new ErrorWithResponse(0, authResponse.getErrors()); assertNull(authResponse.getCardNonce()); assertFalse(authResponse.isSuccess()); assertEquals("Failed to authenticate, please try a different form of payment", errors.getMessage()); }
@Test public void removeListener_noErrorCallbacksReceived() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); BraintreeErrorListener listener = new BraintreeErrorListener() { @Override public void onError(Exception error) { fail("Listener was called"); } }; fragment.addListener(listener); fragment.removeListener(listener); fragment.postCallback(new Exception()); fragment.postCallback(new ErrorWithResponse(400, "")); }
@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()); }