@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()); } } } }
@Test public void parseResponseFailsWithValidationNotAllowed() throws Exception { String baseUrl = "http://example.com/graphql"; HttpClient httpClient = new BraintreeGraphQLHttpClient(baseUrl, TOKENIZATION_KEY); httpClient = HttpClientTestUtils.stubResponse(httpClient, 200, FixturesHelper.stringFromFixture("errors/graphql/validation_not_allowed_error.json")); HttpURLConnection connection = httpClient.init(baseUrl); try { httpClient.parseResponse(connection); fail("No exception was thrown"); } catch (AuthorizationException e) { assertEquals("Validation is not supported for requests authorized with a tokenization key.", e.getMessage()); } }
@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; } }
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 getRewardsBalance_sendsAnalyticsEventOnFailure() { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfiguration) .errorResponse(new AuthorizationException("Bad fingerprint")) .build(); AmericanExpress.getRewardsBalance(fragment, "fake-nonce", "USD"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.start"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.error"); }
@Test public void onActivityResult_withFailedVaultCall_postsCallbackToErrorListener() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization clientToken = Authorization.fromString(stringFromFixture("base_64_client_token.txt")); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(clientToken) .sessionId("session-id") .errorResponse(new AuthorizationException("Bad fingerprint")) .build(); ArgumentCaptor<Exception> responseCaptor = ArgumentCaptor.forClass(Exception.class); Venmo.authorizeAccount(fragment, true); Intent responseIntent = new Intent() .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "nonce"); Venmo.onActivityResult(fragment, Activity.RESULT_OK, responseIntent); verify(fragment).postCallback(responseCaptor.capture()); Exception exception = responseCaptor.getValue(); assertTrue(exception instanceof AuthorizationException); assertEquals("Bad fingerprint", exception.getMessage()); }
@Test public void onActivityResult_withFailedVaultCall_sendsAnalyticsEvent() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization clientToken = Authorization.fromString(stringFromFixture("base_64_client_token.txt")); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(clientToken) .sessionId("session-id") .errorResponse(new AuthorizationException("Bad fingerprint")) .build(); Venmo.authorizeAccount(fragment, true); Intent responseIntent = new Intent() .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "nonce"); Venmo.onActivityResult(fragment, Activity.RESULT_OK, responseIntent); verify(fragment).sendAnalyticsEvent(endsWith("pay-with-venmo.vault.failed")); }