Search in sources :

Example 1 with ApiException

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"));
}
Also used : ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) Test(org.junit.Test)

Example 2 with ApiException

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");
    }
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) Test(org.junit.Test)

Example 3 with ApiException

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"));
}
Also used : HttpException(wiremock.org.apache.http.HttpException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) Test(org.junit.Test)

Example 4 with ApiException

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");
    }
}
Also used : MockContext(com.walmartlabs.concord.sdk.MockContext) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) MissingParameterException(com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) Test(org.junit.Test)

Example 5 with ApiException

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);
}
Also used : ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) ConfigException(com.walmartlabs.concord.plugins.puppet.model.exception.ConfigException) ApiException(com.walmartlabs.concord.plugins.puppet.model.exception.ApiException)

Aggregations

ApiException (com.walmartlabs.concord.plugins.puppet.model.exception.ApiException)6 Test (org.junit.Test)5 CertificateException (java.security.cert.CertificateException)3 MissingParameterException (com.walmartlabs.concord.plugins.puppet.model.exception.MissingParameterException)2 MockContext (com.walmartlabs.concord.sdk.MockContext)2 ConfigException (com.walmartlabs.concord.plugins.puppet.model.exception.ConfigException)1 FileNotFoundException (java.io.FileNotFoundException)1 HttpException (wiremock.org.apache.http.HttpException)1