Package com.test

Source Code of com.test.CRMUtils

package com.test;

import java.io.FileNotFoundException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;

import com.acrm.client.exception.CannotConnectException;
import com.acrm.client.exception.InteractionException;
import com.acrm.client.exception.ServerThrowException;

public class CRMUtils {
  /**
   * 得到最底层引起该异常的异常
   * @param t
   * @return
   */
  public static Throwable getCaused(Throwable t){
    Throwable th1=null,th2=t;
    while(th2!=null && (th1=th2.getCause())!=null){
      th2=th1.getCause();
    }
    return th1==null?th2:th1;
  }
 
  public static Throwable getCausedUntil(Throwable t,Class<? extends Exception> clss){
    Throwable th1=null,th2=t;
    while(th2!=null  && ((th1=th2.getCause())!=null) || clss.isInstance(th2)){
      th2=th1.getCause();
    }
    return th1==null?th2:th1;
  }
 
  public static InteractionException parseException(Throwable t){
    //t.printStackTrace();
    t=CRMUtils.getCaused(t);
    if(t instanceof ServerThrowException){
      return (InteractionException) t;
    }if(t instanceof ConnectException){
      return new CannotConnectException("Connect refused", 2);//连接被拒绝
    }else if(t instanceof SocketTimeoutException){//connect timed out 21    Read timed out 14
      return new CannotConnectException("Connect timed out",t, 1);//连接超时//网络不畅通
    }else if(t instanceof NoRouteToHostException){
      return new CannotConnectException("No routeto host", 0);//没有到主机的路由
    }else if(t instanceof SocketTimeoutException){
      return new CannotConnectException("Read timed out", 1);//读取数据超时
    }else if(t instanceof FileNotFoundException){
      return new ServerThrowException("Cannot connect to Server",t, 0);//连接失败,导致此异常极有可能服务端异常405
    }else{
      return new CannotConnectException("连接失败", t,0);
    }
  }
}
TOP

Related Classes of com.test.CRMUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.