Example usage for java.lang Throwable getCause

List of usage examples for java.lang Throwable getCause

Introduction

In this page you can find the example usage for java.lang Throwable getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:io.coala.enterprise.Actor.java

/**
 * @param actorType the type of {@link Actor} to mimic
 * @param callObserver an {@link Observer} of method call, or {@code null}
 * @return the {@link Proxy} instance//  w w w  .  j a  va 2  s. com
 */
@SuppressWarnings("unchecked")
static <A extends Actor<T>, F extends Fact, T extends F> A proxyAs(final Actor<F> impl,
        final Class<A> actorType, final Observer<Method> callObserver) {
    final A proxy = (A) Proxy.newProxyInstance(actorType.getClassLoader(), new Class<?>[] { actorType },
            (self, method, args) -> {
                try {
                    final Object result = method.isDefault() && Proxy.isProxyClass(self.getClass())
                            ? ReflectUtil.invokeDefaultMethod(self, method, args)
                            : method.invoke(impl, args);
                    if (callObserver != null)
                        callObserver.onNext(method);
                    return result;
                } catch (Throwable e) {
                    if (e instanceof IllegalArgumentException)
                        try {
                            return ReflectUtil.invokeAsBean(impl.properties(), actorType, method, args);
                        } catch (final Exception ignore) {
                            LogUtil.getLogger(Fact.class).warn("{}method call failed: {}",
                                    method.isDefault() ? "default " : "", method, ignore);
                        }
                    if (e instanceof InvocationTargetException)
                        e = e.getCause();
                    if (callObserver != null)
                        callObserver.onError(e);
                    throw e;
                }
            });
    return proxy;
}

From source file:co.com.runt.runistac.logica.TareasLogica.java

/**
 * Metodo recursivo que obtiene la excepcion original
 *//*from  w  w w  .  j  a  v  a 2 s  .c  om*/
private Throwable obtenerUltimaExcepcion(Throwable e) {
    if (e != null && e.getCause() != null) {
        return obtenerUltimaExcepcion(e.getCause());
    } else {
        return e;
    }
}

From source file:com.gs.obevo.db.impl.core.jdbc.DataAccessException.java

/**
 * Retrieve the innermost cause of this exception, if any.
 *
 * @return the innermost exception, or {@code null} if none
 * @since 2.0/*  w  ww  .  j a  v  a  2  s . c  o m*/
 */
public Throwable getRootCause() {
    Throwable rootCause = null;
    Throwable cause = this.getCause();
    while (cause != null && cause != rootCause) {
        rootCause = cause;
        cause = cause.getCause();
    }
    return rootCause;
}

From source file:com.algoTrader.service.ib.IBMarketDataServiceException.java

/**
 * Finds the root cause of the parent exception
 * by traveling up the exception tree/*  w w w.ja  va 2 s  .  co m*/
 */
private static Throwable findRootCause(Throwable th) {
    if (th != null) {
        // Reflectively get any exception causes.
        try {
            Throwable targetException = null;

            // java.lang.reflect.InvocationTargetException
            String exceptionProperty = "targetException";
            if (PropertyUtils.isReadable(th, exceptionProperty)) {
                targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
            } else {
                exceptionProperty = "causedByException";
                //javax.ejb.EJBException
                if (PropertyUtils.isReadable(th, exceptionProperty)) {
                    targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
                }
            }
            if (targetException != null) {
                th = targetException;
            }
        } catch (Exception ex) {
            // just print the exception and continue
            ex.printStackTrace();
        }

        if (th.getCause() != null) {
            th = th.getCause();
            th = findRootCause(th);
        }
    }
    return th;
}

From source file:org.neo4j.ogm.persistence.authentication.AuthenticatingDriverTest.java

@Test
public void testUnauthorizedDriver() {

    session = new SessionFactory(getBaseConfiguration().credentials("", "").build(), "dummy").openSession();

    try (Transaction tx = session.beginTransaction()) {
        tx.commit();/*  w  ww  . ja  va  2 s .c om*/
        fail("Driver should not have authenticated");
    } catch (Exception rpe) {
        Throwable cause = rpe.getCause();
        while (!(cause instanceof HttpResponseException)) {
            cause = cause.getCause();
        }
        assertThat(cause.getMessage().startsWith("Invalid username or password")).isTrue();
    }
}

From source file:org.neo4j.ogm.persistence.authentication.AuthenticatingDriverTest.java

/**
 * @see issue #35//  w  w  w  . j  av  a  2  s. c  om
 */
@Test
public void testInvalidCredentials() {

    session = new SessionFactory(getBaseConfiguration().credentials("neo4j", "invalid_password").build(),
            "dummy").openSession();

    try (Transaction tx = session.beginTransaction()) {
        fail("Driver should not have authenticated");
    } catch (Exception rpe) {
        Throwable cause = rpe.getCause();
        while (!(cause instanceof HttpResponseException)) {
            cause = cause.getCause();
        }
        assertThat(cause.getMessage()).isEqualTo("Invalid username or password.");
    }
}

From source file:fi.hsl.parkandride.front.ExceptionHandlers.java

private Throwable resolveError(Throwable ex) {
    while (ex instanceof ServletException && ex.getCause() != null) {
        ex = ((ServletException) ex).getCause();
    }/*from   w  ww .  ja  va 2  s. co  m*/
    return ex;
}

From source file:net.sourceforge.subsonic.filter.RESTFilter.java

private void handleException(Throwable x, HttpServletRequest request, HttpServletResponse response) {
    if (x instanceof NestedServletException && x.getCause() != null) {
        x = x.getCause();//from  w  w w .j  av  a 2  s . c om
    }

    RESTController.ErrorCode code = (x instanceof ServletRequestBindingException) ? MISSING_PARAMETER : GENERIC;
    String msg = getErrorMessage(x);
    LOG.warn("Error in REST API: " + msg, x);

    try {
        jaxbWriter.writeErrorResponse(request, response, code, msg);
    } catch (Exception e) {
        LOG.error("Failed to write error response.", e);
    }
}

From source file:com.caronic.data.redis.SampleRedisApplicationTests.java

private boolean redisServerRunning(Throwable ex) {
    System.out.println(ex.getMessage());
    if (ex instanceof RedisConnectionFailureException) {
        return false;
    }//from  www .j  a  v a  2s.  co m
    return (ex.getCause() == null || redisServerRunning(ex.getCause()));
}

From source file:com.mustardgrain.solr.SolrClient.java

private static String getNestedErrorMessages(Throwable t) {
    StringBuilder sb = new StringBuilder();

    while (t != null) {
        if (sb.length() > 0)
            sb.append("; caused by: ");

        sb.append(t.getMessage());//  w  ww. ja v a  2s  .co m
        t = t.getCause();
    }

    return sb.toString();
}