use of com.walmartlabs.concord.plugins.puppet.model.exception.ApiException in project concord-plugins by walmartlabs.
the class ApiExceptionTest method testWithStatusCode.
@Test
public void testWithStatusCode() {
ApiException ex = ApiException.buildException(404, "not found");
assertEquals(404, ex.getCode());
assert (ex.getMessage().contains("404") && ex.getMessage().contains("not found"));
ex = ApiException.buildException(504, "timeout");
assertEquals(504, ex.getCode());
assert (ex.getMessage().contains("Server error"));
ex = ApiException.buildException(306, "unused");
assertEquals(306, ex.getCode());
assert (ex.getMessage().contains("Error occurred"));
}
use of com.walmartlabs.concord.plugins.puppet.model.exception.ApiException in project concord-plugins by walmartlabs.
the class PuppetTaskTest method test503.
/**
* 503 errors are retried. The resulting exception will be due to retry max
* being reached, not the 503 error itself.
*/
@Test
public void test503() {
stubFor503();
MockContext ctx = new MockContext(buildDbQueryConfig());
UtilsTest.injectVariable(task, "action", "pql");
ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
try {
task.execute(ctx);
throw new Exception("No exception was thrown in task.");
} catch (ApiException expected) {
assertTrue(expected.getMessage().contains("Retry max reached"));
} catch (Exception e) {
fail("404 error should cause API exception");
}
}
use of com.walmartlabs.concord.plugins.puppet.model.exception.ApiException in project concord-plugins by walmartlabs.
the class ApiExceptionTest method testThrowable.
@Test
public void testThrowable() {
ApiException ex = new ApiException(new HttpException("http error"));
assertTrue(ex.getMessage().contains("http error"));
}
use of com.walmartlabs.concord.plugins.puppet.model.exception.ApiException in project concord-plugins by walmartlabs.
the class PuppetTaskTest method test404.
@Test
public void test404() {
stubFor404();
MockContext ctx = new MockContext(buildDbQueryConfig());
UtilsTest.injectVariable(task, "action", "pql");
ctx.setVariable("queryString", "inventory[certname]{ limit 10 }");
try {
task.execute(ctx);
throw new Exception("No exception was thrown in task.");
} catch (ApiException expected) {
assertEquals(404, expected.getCode());
} catch (Exception e) {
fail("404 error should cause API exception");
}
}
use of com.walmartlabs.concord.plugins.puppet.model.exception.ApiException in project concord-plugins by walmartlabs.
the class PuppetClient method withRetry.
/**
* Executes a Callable until successful, up to a given number of retries
*
* @param retryCount Number of allowed retry attempts
* @param retryInterval Milliseconds to wait between retries
* @param c Callable to execute
* @param <T> Type of Callable
* @return results of the Callable
* @throws ApiException when api call can't be made successfully
*/
private static <T> T withRetry(int retryCount, long retryInterval, Callable<T> c) throws Exception {
Exception exception = null;
int tryCount = 0;
while (!Thread.currentThread().isInterrupted() && tryCount < retryCount) {
try {
// execute it
return c.call();
} catch (ApiException e) {
exception = e;
log.error("call error: '{}'", e.getMessage());
// these errors aren't worth retrying (auth, not found, etc)
if (e.getCode() >= 400 && e.getCode() < 500) {
throw e;
}
} catch (SSLHandshakeException e) {
// probably due to self-signed cert that isn't trusted
log.error("Error during SSL handshake. Likely due to untrusted self-signed certificate.");
throw e;
} catch (Exception e) {
exception = e;
log.error("call error", e);
}
if (tryCount < retryCount) {
// take a break
log.info("retry after {} sec", retryInterval / 1000);
Utils.sleep(1000);
tryCount++;
}
}
// too many attempts, time to give up
if (tryCount == retryCount) {
throw new ApiException(String.format("Retry max reached: %s", exception.getMessage()));
}
// Very unexpected exception
throw new ApiException(exception);
}
Aggregations