package com.czg.utils;

import cn.hutool.core.lang.func.Func0;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;

@Slf4j
public class FunUtils {
    /**
     * 执行方法并捕获异常
     *
     * @param func  方法
     * @param value 失败的值
     */
    public static <T> T safeRun(Func0<T> func, T value) {
        try {
            return func.call();
        } catch (Exception e) {
            log.warn("方法执行失败: {}", e.getMessage());
        }

        return value;
    }

    public static void safeRunVoid(Runnable func, String... msg) {
        try {
            func.run();
        } catch (Exception e) {
            log.warn(msg.length > 0 ? msg[0] : "方法执行失败: {}", e.getMessage());
        }
    }

    public static void safeRunVoid(Runnable func, String errorMsg, Object... args) {
        try {
            func.run();
        } catch (Exception e) {
            log.error(errorMsg, args, e);
        }
    }
}
