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:org.vaadin.spring.security.util.SecurityExceptionUtils.java

private static boolean hasExceptionOfTypeInChain(Class<? extends Throwable> type, Throwable throwable) {
    if (throwable == null) {
        return false;
    } else if (type.isInstance(throwable)) {
        return true;
    } else {//w  w w  . jav  a  2 s  . c  o m
        return hasExceptionOfTypeInChain(type, throwable.getCause());
    }
}

From source file:com.norbl.util.StringUtil.java

private static String throwableToString(Throwable x, String prefix) {

    String mess = x.toString() + "\n\n" + stackTraceToString(x.getStackTrace());
    Throwable cause = x.getCause();
    if (cause != null) {
        mess += "\nCAUSE:\n" + throwableToString(cause, prefix + "    ");
    }/*ww w . j  a  va 2s. c o m*/
    return (mess);
}

From source file:fr.landel.utils.commons.exception.ExceptionUtils.java

/**
 * Returns the original cause. Checks recursively until the root cause was
 * found.// www.j a  v a2s.c o m
 * 
 * <pre>
 * ExceptionUtils.getCauseOrigin(new IllegalArgumentException("param error"));
 * // -&gt; the instance of IllegalArgumentException
 * 
 * ExceptionUtils.getCauseOrigin(new IllegalArgumentException(new IOException("access error")));
 * // -&gt; the instance of IOException
 * </pre>
 * 
 * @param exception
 *            the exception (required, not {@code null})
 * @return the cause, if no cause was found, returns the exception
 */
public static Throwable getCauseOrigin(final Throwable exception) {
    Throwable cause = Objects.requireNonNull(exception, "exception");
    Throwable origin = null;

    while ((cause = cause.getCause()) != null) {
        origin = cause;
    }

    return ObjectUtils.defaultIfNull(origin, exception);
}

From source file:io.servicecomb.swagger.generator.core.unittest.UnitTestSwaggerUtils.java

public static void testException(String expectMsgLevel1, String expectMsgLevel2,
        SwaggerGeneratorContext context, Class<?> cls, String... methods) {
    Throwable exception = getException(context, cls, methods);
    Assert.assertEquals(expectMsgLevel1, exception.getMessage());
    Assert.assertEquals(expectMsgLevel2, exception.getCause().getMessage());
}

From source file:com.wavemaker.common.util.SystemUtils.java

public static Throwable getRootException(Throwable th) {

    while (th.getCause() != null) {
        th = th.getCause();//w ww  . j a  v a2s.  c  o m
    }

    return th;
}

From source file:edu.usc.goffish.gopher.impl.client.GopherClient.java

private static void handleException(String message, Throwable e) {
    logger.log(Level.SEVERE, message + "- cause " + e.getCause());
    throw new RuntimeException(e);

}

From source file:ee.ria.xroad.common.message.SoapParserImpl.java

@SuppressWarnings("unchecked")
static <T> T unmarshalHeader(Class<?> clazz, SOAPHeader soapHeader, boolean checkRequiredFields)
        throws Exception {
    Unmarshaller unmarshaller = JaxbUtils.createUnmarshaller(clazz);

    if (checkRequiredFields) {
        unmarshaller.setListener(new RequiredHeaderFieldsChecker(clazz));
    }//from  w  w w  .  j  av a  2s .  c  om

    unmarshaller.setEventHandler(event -> {
        switch (event.getSeverity()) {
        case ValidationEvent.WARNING:
            return true;
        case ValidationEvent.ERROR:
            Throwable t = event.getLinkedException();

            return !(t instanceof AccessorException && t.getCause() instanceof CodedException);
        case ValidationEvent.FATAL_ERROR:
            return false;
        default:
            return true;
        }
    });

    JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(soapHeader, clazz);
    return jaxbElement.getValue();
}

From source file:Main.java

public static String getCauses(Throwable e) {
    try {/*from   w ww .j  a v a  2 s .c o  m*/
        StringBuilder sb = new StringBuilder();
        while (e != null) {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(e.getClass().getSimpleName());
            e = e.getCause();
        }

        return sb.toString();

    } catch (Throwable derp) {
        return "derp " + derp.getClass().getSimpleName();
    }
}

From source file:io.github.jeddict.collaborate.issues.ExceptionUtils.java

public static void printStackTrace(String errorMessage, final Throwable t, final ModelerFile file) {
    t.printStackTrace();//w  w w .j av a2  s. c o  m
    if (StringUtils.isBlank(errorMessage)) {
        errorMessage = t.getMessage();

        if (StringUtils.isBlank(errorMessage)) {
            if (t.getCause() != null && StringUtils.isNotBlank(t.getCause().getMessage())) {
                errorMessage = t.getCause().getMessage();
            } else if (t.getStackTrace().length > 0) {
                errorMessage = t.getStackTrace()[0].toString();
            }
        }
    }
    final String message = errorMessage;
    LOG.log(Level.ALL, errorMessage, t);
    String content = file != null ? file.getContent() : "";
    SwingUtilities.invokeLater(() -> {
        ExceptionReporterPanel exceptionReporterPanel = new ExceptionReporterPanel(message, t, content);
        exceptionReporterPanel.setVisible(true);
    });
}

From source file:org.gc.networktester.util.Util.java

/** Go up the chain of exception causes, and print a nice trace of that all. */
public static String printException(Exception e) {
    StringBuilder out = new StringBuilder();
    String causePrefix = "";
    Throwable t = e;
    while (t != null) {
        out.append(causePrefix).append("exception: ").append(t).append(" at: ");
        if (t.getCause() == null) {
            // no more cause, print full backtrace now because that's the most interesting exception
            out.append("\n").append(backtraceFull(t.getStackTrace(), 0)).append("\n");
        } else {/*w w  w.j  av a2  s  .c  o m*/
            // there's a cause, print only one line of trace because that is not the most interesting exception
            out.append(t.getStackTrace()[0]).append("\n");
        }
        t = t.getCause();
        // grow the cause prefix 
        causePrefix += "...cause: ";
    }
    return out.toString();
}