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

net.yudichev.jiotty.common.lang.HumanReadableExceptionMessage Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package net.yudichev.jiotty.common.lang;

import com.google.common.base.Throwables;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

public final class HumanReadableExceptionMessage {
    public static String humanReadableMessage(Throwable exception) {
        List causalChain = Throwables.getCausalChain(exception);
        StringBuilder stringBuilder = new StringBuilder(64);
        Throwable parent = null;
        for (Throwable throwable : causalChain) {
            if (parent != null && !exceptionMessage(parent).equals(throwable.toString())) {
                append(stringBuilder, exceptionMessage(parent));
            }
            parent = throwable;
        }
        append(stringBuilder, exceptionMessage(checkNotNull(parent)));

        return stringBuilder.toString();
    }

    private static String exceptionMessage(Throwable exception) {
        return exception instanceof InterruptedException ? "Interrupted" : exception.getMessage();
    }

    private static void append(StringBuilder stringBuilder, String message) {
        if (stringBuilder.length() > 0) {
            stringBuilder.append(": ");
        }
        stringBuilder.append(message);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy