package go.kr.project.api.util;

/**
 * Common helper to extract root-cause message and truncate to DB column limit (default 4000 chars).
 */
public final class ExceptionDetailUtil {

    private ExceptionDetailUtil() {}

    public static String buildForLog(Throwable t) {
        return buildForLog(t, 4000);
    }

    public static String buildForLog(Throwable t, int maxLen) {
        if (t == null) return "오류: unknown";
        Throwable root = t;
        int guard = 0;
        while (root.getCause() != null && root.getCause() != root && guard++ < 20) {
            root = root.getCause();
        }
        String className = root.getClass().getName();
        String message = root.getMessage();
        String detail = (message != null && !message.isEmpty()) ? (className + ": " + message) : root.toString();
        if (detail != null && detail.length() > maxLen) {
            detail = detail.substring(0, maxLen);
        }
        return detail;
    }
}
