@Override protected String parseResponse(HttpURLConnection connection) throws Exception { String response = super.parseResponse(connection); JSONArray errors = new JSONObject(response) .optJSONArray(Keys.ERRORS); if (errors != null) { for (int i = 0; i < errors.length(); i++) { JSONObject error = errors.getJSONObject(i); JSONObject extensions = error.optJSONObject(Keys.EXTENSIONS); String message = Json.optString(error, Keys.MESSAGE, "An Unexpected Exception Occurred"); if (extensions == null) { throw new UnexpectedException(message); } if (Json.optString(extensions, Keys.LEGACY_CODE, "").equals(LegacyErrorCodes.VALIDATION_NOT_ALLOWED)) { throw new AuthorizationException(error.getString(Keys.MESSAGE)); } else if (!Json.optString(extensions, Keys.ERROR_TYPE, "").equals(ErrorTypes.USER)) { throw new UnexpectedException(message); } } throw ErrorWithResponse.fromGraphQLJson(response); } return response; } }
@Test public void parseResponseFailsWithCoercionError() throws Exception { String baseUrl = "http://example.com/graphql"; HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY); httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/coercion_error.json")); HttpURLConnection connection = httpClient.init(baseUrl); try { httpClient.parseResponse(connection); fail("No exception was thrown"); } catch (UnexpectedException e) { assertEquals("Variable 'input' has coerced Null value for NonNull type 'String!'", e.getMessage()); } }
@Test public void parseResponseFailsWithUnknownError() throws Exception { String baseUrl = "http://example.com/graphql"; HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY); httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/unknown_error.json")); HttpURLConnection connection = httpClient.init(baseUrl); try { httpClient.parseResponse(connection); fail("No exception was thrown"); } catch (UnexpectedException e) { assertEquals("An Unexpected Exception Occurred", e.getMessage()); } } }
@Override public void get(String path, HttpResponseCallback callback) { if (path.contains(mTokenizationKey.getConfigUrl())) { callback.failure(new UnexpectedException("Something bad happened")); } } };
@Override public void get(String path, HttpResponseCallback callback) { if (path.contains(mTokenizationKey.getConfigUrl())) { callback.failure(new UnexpectedException("Something bad happened")); } } });
protected String parseResponse(HttpURLConnection connection) throws Exception { int responseCode = connection.getResponseCode(); boolean gzip = "gzip".equals(connection.getContentEncoding()); switch(responseCode) { case HTTP_OK: case HTTP_CREATED: case HTTP_ACCEPTED: return readStream(connection.getInputStream(), gzip); case HTTP_UNAUTHORIZED: throw new AuthenticationException(readStream(connection.getErrorStream(), gzip)); case HTTP_FORBIDDEN: throw new AuthorizationException(readStream(connection.getErrorStream(), gzip)); case HTTP_BAD_REQUEST: case 422: // HTTP_UNPROCESSABLE_ENTITY throw new UnprocessableEntityException(readStream(connection.getErrorStream(), gzip)); case 426: // HTTP_UPGRADE_REQUIRED throw new UpgradeRequiredException(readStream(connection.getErrorStream(), gzip)); case 429: // HTTP_TOO_MANY_REQUESTS throw new RateLimitException("You are being rate-limited. Please try again in a few minutes."); case HTTP_INTERNAL_ERROR: throw new ServerException(readStream(connection.getErrorStream(), gzip)); case HTTP_UNAVAILABLE: throw new DownForMaintenanceException(readStream(connection.getErrorStream(), gzip)); default: throw new UnexpectedException(readStream(connection.getErrorStream(), gzip)); } }
@Test public void getPaymentMethodNonces_throwsAnError() { BraintreeFragment fragment = new MockFragmentBuilder() .errorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); verify(fragment).postCallback(captor.capture()); assertTrue(captor.getValue() instanceof UnexpectedException); }
@Config(sdk = 21) @Test public void deletePaymentMethodNonce_throwsAnError() throws InvalidArgumentException { Authorization authorization = Authorization.fromString(stringFromFixture("client_token.json")); BraintreeFragment fragment = new MockFragmentBuilder() .authorization(authorization) .graphQLErrorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.deletePaymentMethod(fragment, mCardNonce); ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); verify(fragment).postCallback(captor.capture()); PaymentMethodDeleteException paymentMethodDeleteException = (PaymentMethodDeleteException)captor.getValue(); PaymentMethodNonce paymentMethodNonce = paymentMethodDeleteException.getPaymentMethodNonce(); assertEquals(mCardNonce, paymentMethodNonce); }
@Test public void getPaymentMethodNonces_sendsAnAnalyticsEventForErrors() { BraintreeFragment fragment = new MockFragmentBuilder() .errorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); verify(fragment).sendAnalyticsEvent("get-payment-methods.failed"); }
@Config(sdk = 21) @Test public void deletePaymentMethodNonce_sendAnAnalyticsEventForFailure() throws InvalidArgumentException { Authorization authorization = Authorization.fromString(stringFromFixture("client_token.json")); BraintreeFragment fragment = new MockFragmentBuilder() .authorization(authorization) .graphQLErrorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.deletePaymentMethod(fragment, mCardNonce); verify(fragment).sendAnalyticsEvent("delete-payment-methods.failed"); }