Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

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

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:org.dstadler.commons.logging.jdk.PatternFormatter.java

private String getStackLayout(Throwable t, String indenterIn) {
    String indenter = indenterIn + " ";

    StackTraceElement[] ste = t.getStackTrace();
    StringBuilder stack = new StringBuilder(indenter).append(ste[0].toString());
    for (int i = 1; i < ste.length; i++) {
        stack.append("\n").append(indenter).append(ste[i]);
    }/*from   w  w  w.  j a  v a 2 s. c  o m*/

    String innerStack = "";
    if (t.getCause() != null) {
        innerStack = indenter + "Caused by: " + t.getCause().getMessage() + "\n";
        innerStack = innerStack + getStackLayout(t.getCause(), indenter);
    }
    stack.append("\n").append(innerStack);

    return stack.toString();
}

From source file:org.openvpms.web.component.error.ThrowableAdapter.java

/**
 * Constructs a <tt>ThrowableAdapter</tt>.
 *
 * @param exception the exception/*from  w w w.  j a  va 2s  . com*/
 */
public ThrowableAdapter(Throwable exception) {
    type = exception.getClass();
    message = exception.getLocalizedMessage();
    stackTrace = exception.getStackTrace();
    Throwable root = ExceptionUtils.getCause(exception);
    if (root != null) {
        cause = new ThrowableAdapter(root);
    }
}

From source file:ThreadTester.java

public void uncaughtException(Thread t, Throwable e) {
    System.err.printf("%s: %s at line %d of %s%n", t.getName(), e.toString(),
            e.getStackTrace()[0].getLineNumber(), e.getStackTrace()[0].getFileName());
}

From source file:org.apache.tomcat.util.compat.Jdk14Compat.java

/**
 * Print out a partial servlet stack trace (truncating at the last 
 * occurrence of javax.servlet.)./*from   w w  w .  j  av a  2 s. com*/
 */
public String getPartialServletStackTrace(Throwable t) {
    StringBuffer trace = new StringBuffer();
    trace.append(t.toString()).append('\n');
    StackTraceElement[] elements = t.getStackTrace();
    int pos = elements.length;
    for (int i = 0; i < elements.length; i++) {
        if ((elements[i].getClassName().startsWith("org.apache.catalina.core.ApplicationFilterChain"))
                && (elements[i].getMethodName().equals("internalDoFilter"))) {
            pos = i;
        }
    }
    for (int i = 0; i < pos; i++) {
        if (!(elements[i].getClassName().startsWith("org.apache.catalina.core."))) {
            trace.append('\t').append(elements[i].toString()).append('\n');
        }
    }
    return trace.toString();
}

From source file:org.openmrs.module.errorlogging.extension.html.ErrorHandlerExtension.java

private String createReportBtn(Exception exception, HttpServletRequest request) {
    UserContext userContext = (UserContext) request.getSession()
            .getAttribute(WebConstants.OPENMRS_USER_CONTEXT_HTTPSESSION_ATTR);
    if (userContext != null || userContext.getAuthenticatedUser() != null) {
        String reportBugUrl = Context.getAdministrationService()
                .getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_REPORT_BUG_URL);
        String openmrsVersion = OpenmrsConstants.OPENMRS_VERSION;
        String serverInfo = request.getSession().getServletContext().getServerInfo();
        String username = Context.getAuthenticatedUser().getUsername();
        if (StringUtils.isBlank(username)) {
            username = Context.getAuthenticatedUser().getSystemId();
        }// w  ww  .j  a v  a  2s. co  m
        ImplementationId id = Context.getAdministrationService().getImplementationId();
        String implementationId = "";
        if (id != null) {
            implementationId = id.getImplementationId();
            implementationId += " = " + id.getName();
        }
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (Module module : ModuleFactory.getStartedModules()) {
            if (isFirst) {
                sb.append(module.getModuleId()).append(" v").append(module.getVersion());
                isFirst = false;
            } else
                sb.append(", ").append(module.getModuleId()).append(" v").append(module.getVersion());
        }
        String startedModules = sb.toString();
        String errorMessage = exception.toString();
        StackTraceElement[] elements;

        if (exception instanceof ServletException) {
            // It's a ServletException: we should extract the root cause
            ServletException sEx = (ServletException) exception;
            Throwable rootCause = sEx.getRootCause();
            if (rootCause == null)
                rootCause = sEx;
            elements = rootCause.getStackTrace();
        } else {
            // It's not a ServletException, so we'll just show it
            elements = exception.getStackTrace();
        }
        // Collect stack trace for reporting bug description
        StringBuilder description = new StringBuilder("Stack trace:\n");
        for (StackTraceElement element : elements) {
            description.append(element).append("\n");
        }
        String stackTrace = OpenmrsUtil.shortenedStackTrace(description.toString());
        String reportBtn = "<form action=\"" + reportBugUrl + "\" target=\"_blank\" method=\"POST\">"
                + "<input type=\"hidden\" name=\"openmrs_version\" value=\"" + openmrsVersion + "\" />"
                + "<input type=\"hidden\" name=\"server_info\" value=\"" + serverInfo + "\" />"
                + "<input type=\"hidden\" name=\"username\" value=\"" + username + "\" />"
                + "<input type=\"hidden\" name=\"implementationId\" value=\"" + implementationId + "\" />"
                + "<input type=\"hidden\" name=\"startedModules\" value=\"" + startedModules + "\" />"
                + "<input type=\"hidden\" name=\"errorMessage\" value=\"" + errorMessage + "\" />"
                + "<input type=\"hidden\" name=\"stackTrace\" value=\"" + stackTrace + "\" />"
                + "<br/><input type=\"submit\" value=\"Report Problem\"></form>";
        return reportBtn;
    }
    return "";
}

From source file:co.marcin.novaguilds.util.LoggerUtils.java

public static void exception(Throwable e) {
    Throwable cause = e.getCause();
    error("", false);
    error("[NovaGuilds] Severe error: " + e.getClass().getSimpleName(), false);
    error("", false);
    error("Server Information:", false);
    error("  NovaGuilds: #" + VersionUtils.getBuildCurrent() + " (" + VersionUtils.getCommit() + ")", false);
    error("  Storage Type: "
            + (plugin.getConfigManager() == null || plugin.getConfigManager().getDataStorageType() == null
                    ? "null"
                    : plugin.getConfigManager().getDataStorageType().name()),
            false);//from   w  w  w. ja  v a2s  . c o m
    error("  Bukkit: " + Bukkit.getBukkitVersion(), false);
    error("  Java: " + System.getProperty("java.version"), false);
    error("  Thread: " + Thread.currentThread(), false);
    error("  Running CraftBukkit: "
            + Bukkit.getServer().getClass().getName().equals("org.bukkit.craftbukkit.CraftServer"), false);
    error("  Exception Message: ", false);
    error("   " + e.getMessage(), false);
    error("", false);

    error("Stack trace: ", false);
    printStackTrace(e.getStackTrace());
    error("", false);

    while (cause != null) {
        error("Caused by: " + cause.getClass().getName(), false);
        error("  " + cause.getMessage(), false);

        printStackTrace(cause.getStackTrace());
        error("", false);
        cause = cause.getCause();
    }

    error("End of Error.", false);
    error("", false);

    //notify all permitted players
    Message.CHAT_ERROROCCURED.broadcast(Permission.NOVAGUILDS_ERROR);
}

From source file:org.eclipse.jubula.client.ui.rcp.Plugin.java

/**
 * checks if the exception is caused by a known GEF problem
 * @param t the Throwable which was caught in the error handler
 * @return true if the exception was cause by an GEF bug
 *///  ww  w.ja  v a 2  s  . c o  m
public static boolean isGEFException(Throwable t) {
    Throwable work = t;
    do {
        StackTraceElement[] stack = work.getStackTrace();
        for (StackTraceElement el : stack) {
            // check for
            // org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx.addSourceCommands
            if (el.getClassName().indexOf("DragEditPartsTrackerEx") != -1) { //$NON-NLS-1$
                return true;
            }
        }
        work = work.getCause();
    } while (work != null);
    return false;
}

From source file:info.magnolia.testframework.htmlunit.AbstractMagnoliaHtmlUnitTest.java

/**
 * Passing a fake exception might be simpler for some cases.
 * @see #saveToFile(com.gargoylesoftware.htmlunit.Page, StackTraceElement)
 *//*from  w w  w . j  a v  a  2s.com*/
protected void saveToFile(Page page, Throwable fakeException) throws IOException {
    saveToFile(page, fakeException.getStackTrace()[0]);
}

From source file:tools.datasync.db2db.util.ExceptionHandler.java

public void handle(Throwable ex, Level level, String message, Object... params) {

    // TODO: Log all caused by messages also...
    // TODO: Search tools.datasync.db2db package method and log 'at' here...
    StackTraceElement[] stackTraceElements = ex.getStackTrace();
    StackTraceElement top = stackTraceElements[0];

    String clazz = top.getClassName();
    String method = top.getMethodName();
    int line = top.getLineNumber();

    StringBuffer sb = new StringBuffer();
    sb.append(clazz);//  w  ww . ja  v  a 2  s.  com
    sb.append('.');
    sb.append(method);
    sb.append('(');
    sb.append(line);
    sb.append(") : ");
    sb.append(message);
    sb.append(". ");
    sb.append(ex.getMessage());
    sb.append('\n');

    logger.log(level, sb.toString());
}

From source file:ome.services.util.BeanHelper.java

public Exception translateException(Throwable t) {
    if (Exception.class.isAssignableFrom(t.getClass())) {
        return (Exception) t;
    } else {//from   w ww  .  jav  a 2s. co m
        InternalException ie = new InternalException(t.getMessage());
        ie.setStackTrace(t.getStackTrace());
        return ie;
    }
}