Example usage for java.lang Throwable printStackTrace

List of usage examples for java.lang Throwable printStackTrace

Introduction

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

Prototype

public void printStackTrace(PrintWriter s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print writer.

Usage

From source file:Main.java

public static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }//from ww  w.  j ava2  s. co m

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Return exception stack trace as a string
 *
 * @param throwable/*from  w  w  w.  j a  v  a  2 s  .co  m*/
 * @return
 */
public static String getStackTrace(Throwable throwable) {
    Writer writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    throwable.printStackTrace(printWriter);
    return writer.toString();
}

From source file:com.mobicage.rogerthat.util.logging.L.java

public static String getStackTraceString(Throwable t) {
    final Writer w = new StringWriter();
    final PrintWriter pw = new PrintWriter(w);
    t.printStackTrace(pw);
    return w.toString();
}

From source file:com.evolveum.midpoint.gui.api.component.result.OpResult.java

public static OpResult getOpResult(PageBase page, OperationResult result) {
    OpResult opResult = new OpResult();
    Validate.notNull(result, "Operation result must not be null.");
    Validate.notNull(result.getStatus(), "Operation result status must not be null.");

    if (result.getCause() != null && result.getCause() instanceof CommonException) {
        LocalizableMessage localizableMessage = ((CommonException) result.getCause()).getUserFriendlyMessage();
        if (localizableMessage != null) {
            opResult.message = WebComponentUtil.resolveLocalizableMessage(localizableMessage, page);

            // Exclamation code:
            //                String key = localizableMessage.getKey() != null ? localizableMessage.getKey() : localizableMessage.getFallbackMessage();
            //              StringResourceModel stringResourceModel = new StringResourceModel(key, page).setModel(new Model<String>()).setDefaultValue(localizableMessage.getFallbackMessage())
            //            .setParameters(localizableMessage.getArgs());
            //              opResult.message = stringResourceModel.getString();
        }/*  www.ja v a  2  s.  c o m*/
    }

    if (opResult.message == null) {
        opResult.message = result.getMessage();
    }
    opResult.operation = result.getOperation();
    opResult.status = result.getStatus();
    opResult.count = result.getCount();
    opResult.userFriendlyMessage = result.getUserFriendlyMessage();

    if (result.getCause() != null) {
        Throwable cause = result.getCause();
        opResult.exceptionMessage = cause.getMessage();

        Writer writer = new StringWriter();
        cause.printStackTrace(new PrintWriter(writer));
        opResult.exceptionsStackTrace = writer.toString();
    }

    if (result.getParams() != null) {
        for (Map.Entry<String, Collection<String>> entry : result.getParams().entrySet()) {
            String paramValue = null;
            Collection<String> values = entry.getValue();
            if (values != null) {
                paramValue = values.toString();
            }

            opResult.getParams().add(new Param(entry.getKey(), paramValue));
        }
    }

    if (result.getContext() != null) {
        for (Map.Entry<String, Collection<String>> entry : result.getContext().entrySet()) {
            String contextValue = null;
            Collection<String> values = entry.getValue();
            if (values != null) {
                contextValue = values.toString();
            }

            opResult.getContexts().add(new Context(entry.getKey(), contextValue));
        }
    }

    if (result.getSubresults() != null) {
        for (OperationResult subresult : result.getSubresults()) {
            OpResult subOpResult = OpResult.getOpResult(page, subresult);
            opResult.getSubresults().add(subOpResult);
            subOpResult.parent = opResult;
            if (subOpResult.getBackgroundTaskOid() != null) {
                opResult.backgroundTaskOid = subOpResult.getBackgroundTaskOid();
            }
        }
    }

    if (result.getBackgroundTaskOid() != null) {
        opResult.backgroundTaskOid = result.getBackgroundTaskOid();
    }

    try {
        OperationResultType resultType = result.createOperationResultType();
        ObjectFactory of = new ObjectFactory();
        opResult.xml = page.getPrismContext().xmlSerializer().serialize(of.createOperationResult(resultType));
    } catch (SchemaException | RuntimeException ex) {
        String m = "Can't create xml: " + ex;
        //         error(m);
        opResult.xml = "<?xml version='1.0'?><message>" + StringEscapeUtils.escapeXml(m) + "</message>";
        //            throw ex;
    }
    return opResult;
}

From source file:mil.dod.th.ose.junit4xmltestrunner.ant.XMLJUnitResultFormatter.java

/**
 * Converts the stack trace from the {@link Throwable} item to a string. 
 * @param t//from w  w  w.  j  a va 2s .  c  o m
 *      {@link Throwable} object containing the stack trace
 * @return
 *      string representation of the stack trace
 */
public static String getStackTrace(final Throwable t) {
    final StringWriter sw = new StringWriter();
    try (final PrintWriter pw = new PrintWriter(sw, true)) {
        t.printStackTrace(pw);
        pw.flush();
    }
    return sw.toString();
}

From source file:app.web.Application2ITCase.java

private static String throwableToString(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    return sw.toString();
}

From source file:edu.temple.cis3238.wiki.utils.StringUtils.java

/**
 *
 * @param e/*  w  w w.jav a  2  s  .c om*/
 * @return
 */
public static final String stackStraceAsStringDetails(final Throwable e) {
    StringBuilder sb = new StringBuilder("");
    if (e != null) {
        final StringWriter stringWriter = new StringWriter();
        try {
            e.printStackTrace(new PrintWriter(stringWriter));
            sb.append(stringWriter.toString());
            sb.append("\nCause:");
            sb.append(e);
            Throwable t = e.getCause();
            while (t != null) {
                sb.append(t);
                sb.append("\n");
                t = t.getCause();
            }
        } catch (Exception exc) {
            sb.append(e.getMessage());
        } finally {
            return StringUtils.toS(sb.toString(), "Sorry.. full stack trace not available");
        }

    } else {
        LOG.warning("Exception null printing stack track");
        return "Null exception";
    }
}

From source file:org.openiot.gsn.utils.GSNMonitor.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter, true);
    t.printStackTrace(printWriter);
    printWriter.flush();//from w w  w . j  a v a 2 s .co m
    stringWriter.flush();
    return stringWriter.toString();
}

From source file:Main.java

/**
 * Copied from "android.util.Log.getStackTraceString()" in order to avoid usage of Android stack
 * in unit tests.// www . j  a va  2s.co  m
 *
 * @return Stack trace in form of String
 */
static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

From source file:edu.samplu.common.ITUtil.java

/**
 * Write the given stack trace into a String remove the ats in an attempt to not cause Jenkins problems.
 * @param throwable whose stack trace to return
 * @return String of the given throwable's stack trace.
 *///from   w  ww. jav a2 s  .  c o  m
public static String stackTrace(Throwable throwable) {
    StringWriter wrt = new StringWriter();
    PrintWriter pw = new PrintWriter(wrt);
    throwable.printStackTrace(pw);
    pw.flush();
    return wrt.toString().replace(" at ", "");
}