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:com.dianping.avatar.cache.util.CacheMonitorUtil.java

private static String getErrorString(Throwable throwable) {
    OutputStream out = null;/*from  w  ww  .  j  a  v a  2  s  .  c om*/
    PrintWriter writer = null;
    try {
        out = new ByteArrayOutputStream(3000);
        writer = new PrintWriter(out);
        throwable.printStackTrace(writer);
        writer.flush();
        return out.toString();
    } catch (Exception e2) {
        return throwable.getMessage();
    } finally {
        if (writer != null) {
            writer.close();
        }
        out = null;
        writer = null;
    }
}

From source file:net.sourceforge.jweb.maven.mojo.InWarMinifyMojo.java

public static String buildStack(Throwable t) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(arrayOutputStream);
    t.printStackTrace(printStream);
    String ret = arrayOutputStream.toString();
    try {//www  . j av  a 2  s  .c  om
        arrayOutputStream.close();
        printStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:com.evolveum.midpoint.web.component.message.OpResult.java

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

    opResult.message = result.getMessage();
    opResult.operation = result.getOperation();
    opResult.status = result.getStatus();
    opResult.count = result.getCount();/*  w  w w . ja va2 s  .c o m*/

    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, Serializable> entry : result.getParams().entrySet()) {
            String paramValue = null;
            Object value = entry.getValue();
            if (value != null) {
                paramValue = value.toString();
            }

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

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

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

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

    try {
        OperationResultType resultType = result.createOperationResultType();
        ObjectFactory of = new ObjectFactory();
        opResult.xml = page.getPrismContext().serializeAtomicValue(of.createOperationResult(resultType),
                PrismContext.LANG_XML);
    } 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:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

private static JSONObject toJSONError(Throwable x, JSONObject log) {
    final StringWriter stackTrace = new StringWriter();
    x.printStackTrace(new PrintWriter(stackTrace));
    final JSONObject error = new JSONObject();
    try {/*from   w  w w.  j a  v a  2  s  . com*/
        error.put("error", x.getClass().getSimpleName());
        error.put("exception", x.getClass().getName());
        error.put("message", x.getLocalizedMessage());
        error.put("stackTrace", stackTrace.toString());
        if (log != null) {
            error.put("log", log);
        }
    } catch (JSONException e) {
        throw new AssertionError(e);
    }
    return error;
}

From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobAlertImpl.java

protected static void attachException(MimeMessageHelper messageHelper, ExceptionInfo exceptionInfo)
        throws MessagingException {
    Throwable exception = exceptionInfo.getException();
    if (exception == null) {
        return;//from   ww w.j a  v  a  2 s  .  co  m
    }

    ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
    PrintStream printOut = new PrintStream(bufOut);
    exception.printStackTrace(printOut);
    printOut.flush();

    String attachmentName = "exception_" + System.identityHashCode(exception) + ".txt";
    messageHelper.addAttachment(attachmentName, new ByteArrayResource(bufOut.toByteArray()));
}

From source file:Main.java

/**
 * Returns a stack trace of the {@code Throwable} as a {@code String}.
 * // w w w  .ja  v  a  2 s.  c  om
 * @param throwable
 *          The throwable for which to create the stack trace.
 * @param expectNull
 *          True if null should be returned when {@code throwable} is null or
 *          false to return "" when {@code throwable} is null
 * @return null if {@code throwable} is null and {@code expectNull} is true,
 *         "" if {@code throwable} is null and {@code expectNull} is false,
 *         otherwise the stack trace for {@code throwable}
 */
public static String stackTraceToString(final Throwable throwable, final boolean expectNull) {
    if (throwable == null) {
        if (expectNull == true) {
            return null;
        }

        return "";
    }

    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    throwable.printStackTrace(printWriter);
    printWriter.close();
    return stringWriter.toString();
}

From source file:com.amazonaws.eclipse.core.diagnostic.utils.AwsPortalFeedbackFormUtils.java

public static String getStackTraceFromThrowable(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    t.printStackTrace(pw);
    pw.println();//w  ww. j a v  a2s. c om

    if (t.getCause() != null) {
        t.getCause().printStackTrace(pw);
    }

    return sw.toString();
}

From source file:io.cloudslang.content.database.utils.SQLUtils.java

public static String exceptionToString(Throwable e) {
    // Print the stack trace into an in memory string
    StringWriter writer = new StringWriter();
    e.printStackTrace(new java.io.PrintWriter(writer));

    // Process the stack trace, remove the FIRST null character
    return writer.toString().replace("" + (char) 0x00, "");
}

From source file:marytts.server.http.MaryHttpServerUtils.java

public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) {
    int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    response.setStatusCode(status);/* w  ww  . j  av  a  2s .  c  om*/
    StringWriter sw = new StringWriter();
    exception.printStackTrace(new PrintWriter(sw, true));
    String logMessage = (message != null ? message + "\n" : "") + sw.toString();
    logger.debug("Returning HTTP status " + status + ": " + logMessage);
    try {
        NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>"
                + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8");
        entity.setContentType("text/html; charset=UTF-8");
        response.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
    }
}

From source file:axiom.util.Logger.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    t.printStackTrace(writer);
    writer.close();/*from   w  ww . ja  v a2  s. com*/
    return stringWriter.toString();
}