package com.face.util;


import com.face.global.enums.CommonCode;
import com.face.global.exception.BusinessException;
import com.face.global.language.LanguageStore;
import io.micrometer.common.util.StringUtils;

public class SystemExceptionUtils {

    public static BusinessException buildBusinessException(String code) {
        return new BusinessException(code,
                MessageUtils.getMessage(code, null, LanguageStore.getLocale()));
    }

    public static BusinessException buildBusinessException(String code, Object... args) {
        return new BusinessException(code, MessageUtils.getMessage(code, args, LanguageStore.getLocale()));
    }

    public static BusinessException buildBusinessException(String code, Object[] args, Throwable throwable) {
        return new BusinessException(code,
                MessageUtils.getMessage(code, args, LanguageStore.getLocale()), throwable);
    }


    public static BusinessException buildBusinessException(String code, Throwable throwable) {
        return new BusinessException(code, MessageUtils.getMessage(code, null, LanguageStore.getLocale()), throwable);
    }

    public static BusinessException buildBusinessException(String code, String message, Throwable throwable) {
        return new BusinessException(code, message, throwable);
    }

    public static BusinessException buildBusinessException(Throwable throwable) {
        if (throwable instanceof BusinessException) {
            return (BusinessException) throwable;
        }
        return buildBusinessException(CommonCode.ERROR.getCode(), throwable);
    }

    public static BusinessException buildBusinessExceptionForMessage(String code, String message) {
        return new BusinessException(StringUtils.isBlank(code) ? CommonCode.ERROR.getCode() : code, message);
    }
}
