All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.yandex.qatools.camelot.util.ExceptionUtil Maven / Gradle / Ivy

There is a newer version: 2.5.4
Show newest version
package ru.yandex.qatools.camelot.util;

/**
 * @author Ilya Sadykov
 */
public abstract class ExceptionUtil {

    ExceptionUtil() {
    }

    public static String formatStackTrace(Throwable exc) {
        Throwable cause = exc;
        StringBuilder builder = new StringBuilder();
        while (cause != null) {
            builder.append("Caused by ").append(cause.toString()).append(": ").append(cause.getMessage()).
                    append("\n");
            builder.append(formatStackTrace(cause.getStackTrace()));
            cause = cause.getCause();
        }
        return builder.toString();
    }


    public static String formatStackTrace(StackTraceElement[] stackTraceElements) {
        StringBuilder builder = new StringBuilder();
        for (StackTraceElement element : stackTraceElements) {
            builder.append("\tat ").append(element.getClassName()).
                    append("<").append(element.getMethodName()).append(">");
            builder.append("(").append(element.getFileName());
            if (element.getLineNumber() > 0) {
                builder.append(":").append(element.getLineNumber());
            }
            builder.append(")");
            builder.append("\n");
        }
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy