package com.sys.core;


import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.sys.core.exception.BusinessException;
import com.sys.core.model.DataBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ExceptionInterceptor implements Interceptor {
    Log log = LogFactory.getLog(this.getClass());

    public void intercept(Invocation inv) {
        Controller controller = inv.getController();
        //HttpServletRequest request = controller.getRequest();
        try {
            inv.invoke();
        }catch(RuntimeException e){
        	log.error("错误", e);
            //e.printStackTrace();
            DataBean dBean = new DataBean();
    		dBean.setCode(-1);
    		dBean.setMsg(e.getMessage());
    		//返回异常信息，json格式
    		controller.renderJson(dBean);
//        } catch (BusinessException e) {
//            log.error("错误", e);
//           // e.printStackTrace();
//             DataBean dBean = new DataBean();
//    		dBean.setCode(-1);
//    		dBean.setMsg(e.getMessage());
//    		//返回异常信息，json格式
//    		controller.renderJson(dBean);

        } catch (Exception e) {
            log.error("程序异常", e);
            e.printStackTrace();
            DataBean dBean = new DataBean();
    		dBean.setCode(-1);
    		dBean.setMsg("系统异常，错误原因："+e.getMessage());
    		//返回异常信息，json格式
    		controller.renderJson(dBean);
        }catch (Throwable e) {

            log.error("程序异常", e);
            e.printStackTrace();
            DataBean dBean = new DataBean();
    		dBean.setCode(-1);
    		dBean.setMsg("系统异常，错误原因："+e.getMessage());
    		//返回异常信息，json格式
    		controller.renderJson(dBean);
        }
    }

}
