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:it.cnr.icar.eric.client.ui.thin.OutputExceptions.java

/**
 * Find a string which should provide most information about a Throwable.
 *
 * @param t Throwable of interest//from  w  w  w. j  a  v  a 2  s  .c  o m
 * @param forLog If true, return more information; going into log
 * @return String message describing original cause for t
 */
private static String getCause(Throwable t, boolean forLog) {
    // Find all causes for this exception
    ArrayList<Throwable> causes = new ArrayList<Throwable>();
    while (null != t) {
        causes.add(t);
        t = t.getCause();
    }
    Collections.reverse(causes);

    // Work backwards through the causes to find the original
    // (hopefully most informative) detail string
    Iterator<Throwable> iter = causes.iterator();
    String msg = null;
    while (iter.hasNext() && null == msg) {
        t = iter.next();
        if (null != t.getMessage()) {
            // Detail string exists
            if (forLog) {
                // Always get class information as well
                msg = t.toString();
            } else {
                msg = goodMessage(t);
            }
        }
    }

    // No detail found, use class information about root cause
    if (null == msg) {
        msg = (causes.get(0)).toString();
    }

    return msg;
}

From source file:org.eclipse.cft.server.core.internal.CloudErrorUtil.java

public static boolean isWebsocketDeploymentException(CoreException e) {
    if (e == null || e.getCause() == null) {
        return false;
    }//from w w w .  j  ava2 s  .  c o  m
    Throwable t = e.getCause();
    return t instanceof DeploymentException || t.getCause() instanceof DeploymentException;
}

From source file:org.artifactory.util.HttpClientUtils.java

/**
 * @param e The throwable to inspect for the error message
 * @return Most fitting error message for the given throwable. Tries to prevent empty exception messages.
 */// ww  w  .  j  a v a2  s.c  om
public static String getErrorMessage(Throwable e) {
    if (e == null) {
        return null;
    }
    String message = e.getMessage(); // default message
    if (e instanceof UnknownHostException) {
        message = "Unknown host - " + e.getMessage();
    } else if (e instanceof ClientProtocolException) {
        // ClientProtocolException doesn't return a message but holds the cause with the message
        if (e.getCause() != null) {
            message = e.getCause().getMessage();
        }
    }
    if (StringUtils.isBlank(message)) {
        message = e.getClass().toString();
    }
    return message;
}

From source file:org.paxml.launch.Paxml.java

public static String getCause(Throwable t) {
    String msg = null;/*from   w w  w  .jav  a 2 s .  c o m*/

    while (t != null && StringUtils.isNotBlank((msg = t.getMessage()))) {
        t = t.getCause();
    }
    return msg;
}

From source file:it.cnr.icar.eric.client.ui.thin.OutputExceptions.java

/**
 * Log exceptions and display them to user in a consistent fashion.
 *
 * @param log Log where error or warning should be output
 * @param logMsg Context message to log with t
 * @param displayMsg Context message to display to user with t
 * @param t Throwable to log about and optionally display
 * @param display Output information about this Throwable on page?
 * @param warning If true, log warning rather than error
 */// w  ww.j a  v  a 2s .c  o m
private static void display(Log log, String logMsg, String displayMsg, Throwable t, boolean display,
        boolean warning) {
    // When not given a Msg, should we provide context for cause?
    boolean needContext = (null != t.getMessage() && null != t.getCause());

    // Log a fair amount of information unconditionally
    if (null == logMsg && null != displayMsg) {
        logMsg = displayMsg;
    }
    if (log.isDebugEnabled()) {
        // Include full traceback in logging output
        if (null == logMsg) {
            if (warning) {
                log.warn(t.toString(), t);
            } else {
                log.error(t.toString(), t);
            }
        } else {
            if (warning) {
                log.warn(logMsg, t);
            } else {
                log.error(logMsg, t);
            }
        }
    } else {
        if (null == logMsg) {
            if (needContext) {
                if (warning) {
                    log.warn(t.toString());
                } else {
                    log.error(t.toString());
                }
            }
        } else {
            if (warning) {
                log.warn(logMsg);
            } else {
                log.error(logMsg);
            }
        }
        if (warning) {
            log.error(getCause(t, true));
        } else {
            log.error(getCause(t, true));
        }
    }

    // Conditionally display a subset of the above information to the user
    if (display) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage.Severity severity = (warning ? FacesMessage.SEVERITY_ERROR : FacesMessage.SEVERITY_WARN);

        if (null == displayMsg && null != logMsg) {
            displayMsg = logMsg;
        }
        if (null == displayMsg) {
            if (needContext) {
                context.addMessage(null, new FacesMessage(severity, goodMessage(t), null));
            }
        } else {
            context.addMessage(null, new FacesMessage(severity, displayMsg, null));
        }
        context.addMessage(null, new FacesMessage(severity, getCause(t, false), null));
    }
}

From source file:cc.sion.core.utils.Exceptions.java

/**
 * ??cause. ??.//  w  ww.  j  a va2  s  . co  m
 *
 * FutureExecutionException  ??InvocationTargetException ?Cause
 *
 * ?? unchecked() UncheckedException??.
 *
 * from Quasar and Tomcat's ExceptionUtils
 */
public static Throwable unwrap(Throwable t) {
    if (t instanceof java.util.concurrent.ExecutionException
            || t instanceof java.lang.reflect.InvocationTargetException || t instanceof UncheckedException) {
        return t.getCause();
    }

    return t;
}

From source file:com.salesforce.ide.core.internal.utils.ForceExceptionUtils.java

private static String getUnknownHostExceptionMessage(StringBuffer strBuff, ConnectorConfig connectorConfig,
        Throwable th) {
    if (strBuff == null) {
        strBuff = new StringBuffer();
    }//from  w w  w  . j  a v  a2 s  . c om

    strBuff.append("\n\nUnknown host: ").append(getStrippedRootCauseMessage(th.getCause()));

    if (connectorConfig != null && connectorConfig.getProxy() != null) {
        strBuff.append("\n\n").append(Messages.getString("Proxy.CheckSettings.message"));
    }

    return strBuff.toString();
}

From source file:com.tc.management.JMXConnectorProxy.java

static boolean isAuthenticationException(IOException ioe) {
    Throwable t = ioe;

    while (t != null) {
        if (t instanceof NotSerializableException) {
            String detailMessage = t.getMessage();
            if ("com.sun.jndi.ldap.LdapCtx".equals(detailMessage)) {
                return true;
            }/*from www.  j  a  v a2s  .com*/
        }
        t = t.getCause();
    }

    return false;
}

From source file:de.micromata.genome.logging.LoggableExceptionHandlingUtil.java

/**
 * Gets the root cause./*from www.j a  v a2  s . c om*/
 *
 * @param e the e
 * @return the root cause
 */
public static Throwable getRootCause(Throwable e) {
    if (e == null) {
        return null;
    }
    if (e instanceof ServletException) {
        return ((ServletException) e).getRootCause();
    }

    if (e instanceof InvocationTargetException) {
        return ((InvocationTargetException) e).getTargetException();
    }
    return e.getCause();
}

From source file:com.codelanx.codelanxlib.util.exception.Exceptions.java

/**
 * Creates a readable stack trace from a passed {@link Throwable}. This
 * method will reproduce the same output that
 * {@link Throwable#printStackTrace()} would output
 *
 * @since 0.1.0// w  w w . j  a  v a 2s . c om
 * @version 0.1.0
 *
 * @param t The {@link Throwable} to make readable
 * @return A string representing the entire stack trace
 */
public static String readableStackTrace(Throwable t) {
    StringBuilder sb = new StringBuilder();
    StackTraceElement[] trace = t.getStackTrace();
    for (StackTraceElement elem : trace) {
        sb.append("\tat ").append(elem).append('\n');
    }
    if (t.getCause() != null) {
        Exceptions.readableStackTraceAsCause(sb, t.getCause(), trace);
    }
    return sb.toString();
}