package com.foreverwin.mesnac.common.util;

import com.foreverwin.modular.core.exception.BusinessException;
import com.sap.me.frame.BasicBOBeanException;
import com.sap.me.frame.transitionutils.Exceptions;
import com.sap.me.frame.utils.I18nUtility;

import java.lang.reflect.UndeclaredThrowableException;

/**
 * 描述: 调用标准API异常处理工具类
 * 作者: Michael.Liu
 * 时间: 2020-12-08 08:30
 */
public class ExceptionUtil {

    public static void throwException(Exception ex) {
        String message;
        if (ex instanceof com.sap.me.frame.domain.BusinessException) {
            message = I18nUtility.getMessageForException((com.sap.me.frame.domain.BusinessException) ex);
        } else if (ex instanceof BasicBOBeanException) {
            message = I18nUtility.getMessageForException(Exceptions.convert((BasicBOBeanException) ex));
        } else if (ex instanceof UndeclaredThrowableException) {
            message = ((UndeclaredThrowableException) ex).getUndeclaredThrowable().getLocalizedMessage();
        } else {
            message = ex.getLocalizedMessage();
        }
        throw BusinessException.build(message);
    }

    public static String getExceptionMsg(Exception ex) {
        String message;
        if (ex instanceof com.sap.me.frame.domain.BusinessException) {
            message = I18nUtility.getMessageForException((com.sap.me.frame.domain.BusinessException) ex);
        } else if (ex instanceof BasicBOBeanException) {
            message = I18nUtility.getMessageForException(Exceptions.convert((BasicBOBeanException) ex));
        } else if (ex instanceof UndeclaredThrowableException) {
            message = ((UndeclaredThrowableException) ex).getUndeclaredThrowable().getLocalizedMessage();
        } else {
            message = ex.getLocalizedMessage();
        }
        return message;
    }
}
