package com.huawei.esdk.fusionmanager.local.impl.utils;

import java.lang.reflect.InvocationTargetException;

import javax.xml.ws.soap.SOAPFaultException;

import org.apache.log4j.Logger;

import com.huawei.esdk.fusionmanager.local.bean.FMSDKResponse;
import com.huawei.esdk.fusionmanager.local.impl.constant.FMSDKErrorCode;

/**
 * 异常处理类
 * @author k00207574
 * @since eSDK Cloud V100R003C20
 */
public abstract class ExceptionUtils
{
    private static final Logger LOGGER = Logger.getLogger(ExceptionUtils.class);
    
    public static void processSoapException(FMSDKResponse<? extends Object> resp, Exception e)
    {
        LOGGER.error("Exception:",e);
        if (e instanceof SOAPFaultException)
        {
            String faultCodeString = ((SOAPFaultException)e).getFault().getFaultCode();
            String faultString = ((SOAPFaultException)e).getFault().getFaultString();
            if (faultCodeString.contains(":"))
            {
                if (faultCodeString.equals("soap:Client") && faultString.contains("Unmarshalling Error"))
                {
                    resp.setResultCode(FMSDKErrorCode.SDK_DATA_INCORRECT_ERRORCODE);
                }
                else if (faultString.contains("Connection refused: connect")
                    || faultString.contains("Connection reset")) // || e.getMessage().contains("Could not send Message")
                {
                    resp.setResultCode(FMSDKErrorCode.ERROR_CODE_NETWORK_ERROR);
                }
                else
                {
                    resp.setResultCode(FMSDKErrorCode.NATIVE_EXCEPTION);
                }
            }
            else
            {
                resp.setResultCode(FMSDKErrorCode.NATIVE_EXCEPTION);
            }
        }
        else if ((e instanceof IllegalAccessException) || (e instanceof InvocationTargetException))
        {
            resp.setResultCode(FMSDKErrorCode.NATIVE_EXCEPTION);
        }
    }
}
