use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.
the class ConfigurationManagerUnitTest method isFetchingConfiguration_isFalseInErrorCallback.
@Test(timeout = 1000)
public void isFetchingConfiguration_isFalseInErrorCallback() throws InterruptedException {
when(mBraintreeFragment.getHttpClient()).thenReturn(new BraintreeHttpClient(mTokenizationKey) {
@Override
public void get(String path, HttpResponseCallback callback) {
if (path.contains(mTokenizationKey.getConfigUrl())) {
callback.failure(new UnexpectedException("Something bad happened"));
}
}
});
ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
fail("Success listener should not have been called for bad request");
}
}, new BraintreeResponseListener<Exception>() {
@Override
public void onResponse(Exception e) {
assertFalse(ConfigurationManager.isFetchingConfiguration());
mCountDownLatch.countDown();
}
});
mCountDownLatch.await();
}
use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.
the class ConfigurationManagerUnitTest method getConfiguration_callsErrorListenerWhenHttpFails.
@Test(timeout = 1000)
public void getConfiguration_callsErrorListenerWhenHttpFails() throws InterruptedException {
BraintreeHttpClient fakeClient = new BraintreeHttpClient(mTokenizationKey) {
@Override
public void get(String path, HttpResponseCallback callback) {
if (path.contains(mTokenizationKey.getConfigUrl())) {
callback.failure(new UnexpectedException("Something bad happened"));
}
}
};
when(mBraintreeFragment.getHttpClient()).thenReturn(fakeClient);
ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() {
@Override
public void onConfigurationFetched(Configuration configuration) {
fail("Success listener should not have been called for bad request");
}
}, new BraintreeResponseListener<Exception>() {
@Override
public void onResponse(Exception e) {
assertTrue(e instanceof UnexpectedException);
assertEquals("Something bad happened", e.getMessage());
mCountDownLatch.countDown();
}
});
mCountDownLatch.await();
}
use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.
the class BraintreeGraphQLHttpClientTest method parseResponseFailsWithCoercionError.
@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("An unexpected error occurred", e.getMessage());
}
}
use of com.braintreepayments.api.exceptions.UnexpectedException in project braintree_android by braintree.
the class BraintreeGraphQLHttpClient method parseResponse.
@Override
protected String parseResponse(HttpURLConnection connection) throws Exception {
String response = super.parseResponse(connection);
JSONArray errors = new JSONObject(response).optJSONArray(ErrorWithResponse.GRAPHQL_ERRORS_KEY);
if (errors != null) {
for (int i = 0; i < errors.length(); i++) {
JSONObject error = errors.getJSONObject(i);
JSONObject extensions = error.optJSONObject("extensions");
if (extensions == null) {
throw new UnexpectedException("An unexpected error occurred");
}
if (Json.optString(extensions, "legacyCode", "").equals("50000")) {
throw new AuthorizationException(error.getString("message"));
} else if (!Json.optString(extensions, "errorType", "").equals("user_error")) {
throw new UnexpectedException("An unexpected error occurred");
}
}
throw ErrorWithResponse.fromGraphQLJson(response);
}
return response;
}
Aggregations