package com.atlassian.bitbucket.util;

import com.google.common.base.Throwables;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nullable;

public class ErrorUtils {

    protected ErrorUtils() {
        throw new UnsupportedOperationException(getClass().getName() +
                " is a utility class and should not be instantiated");
    }

    /**
     * @param throwable the throwable to extract a localised root cause message from
     * @return the localized message of the root cause of {@code throwable}
     */
    @Nullable
    public static String getLocalizedMessageOfRootCause(@Nullable Throwable throwable) {
        if (throwable == null) {
            return null;
        }

        Throwable cause = Throwables.getRootCause(throwable);
        return StringUtils.defaultString(cause.getLocalizedMessage(), cause.toString());
    }
}
