package com.matrix.core.exception; 
 | 
  
 | 
import com.alibaba.fastjson.support.spring.FastJsonJsonView; 
 | 
import com.matrix.core.constance.MatrixConstance; 
 | 
import com.matrix.core.constance.SystemErrorCode; 
 | 
import com.matrix.core.tools.*; 
 | 
import com.matrix.system.common.bean.ProjException; 
 | 
import com.matrix.system.common.bean.SysUsers; 
 | 
import com.matrix.system.common.dao.ProjExceptionDao; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.web.servlet.HandlerExceptionResolver; 
 | 
import org.springframework.web.servlet.ModelAndView; 
 | 
  
 | 
import javax.servlet.http.HttpServletRequest; 
 | 
import javax.servlet.http.HttpServletResponse; 
 | 
import java.io.PrintWriter; 
 | 
import java.io.StringWriter; 
 | 
import java.util.*; 
 | 
  
 | 
/** 
 | 
 * @author 姜友瑶 
 | 
 * @description 全局异常处理类 
 | 
 * @data 2016-06-26 
 | 
 */ 
 | 
@Service("GlobleExceptionResolver") 
 | 
public class GlobleExceptionResolver implements HandlerExceptionResolver { 
 | 
  
 | 
    private static final String TRUE = "true"; 
 | 
  
 | 
    /** 
 | 
     * 忽略一些特定的异常 
 | 
     */ 
 | 
    private static final List EXCLUDE_EXCEPTION = new ArrayList(Arrays.asList("java.io.IOException: Broken pipe")); 
 | 
  
 | 
  
 | 
  
 | 
    @Override 
 | 
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, 
 | 
                                         Exception ex) { 
 | 
        ModelAndView model = new ModelAndView(); 
 | 
        FastJsonJsonView view = new FastJsonJsonView(); 
 | 
        Map<String, Object> attr = new HashMap<>(10); 
 | 
  
 | 
        if (ex instanceof GlobleException) { 
 | 
            // 如果是内部全局异常 
 | 
            LogUtil.warn("#程序抛出全局异常#", ex); 
 | 
            GlobleException globleException = (GlobleException) ex; 
 | 
            attr.put("status", globleException.getErrorCode()); 
 | 
            attr.put("info", globleException.getMessage()); 
 | 
  
 | 
        } else { 
 | 
  
 | 
            // 非内部异常 
 | 
            LogUtil.error("#程序抛出未捕获异常#", ex); 
 | 
            attr.put("status", 999999); 
 | 
            attr.put("info", InternationaUtil.getMesssge(SystemErrorCode.SYSTEM_UNKNOW_ERROR)); 
 | 
            // 发送异常信息到管理群 
 | 
            // 加上请求接口路径 
 | 
            String requestUrl = ""; 
 | 
            if (null != request) { 
 | 
                requestUrl = WebUtil.getLocation(); 
 | 
            } 
 | 
            sendNoticeToAdmin(ex, MdcUtil.getMdc(), requestUrl); 
 | 
  
 | 
        } 
 | 
        attr.put("MDC", MdcUtil.getMdc()); 
 | 
        view.setAttributesMap(attr); 
 | 
        model.setView(view); 
 | 
        return model; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 发送异常信息到管理群 
 | 
     * 
 | 
     * @param ex 
 | 
     * @author JIANGYOUYAO 
 | 
     * @email 935090232@qq.com 
 | 
     * @date 2018年5月9日 
 | 
     */ 
 | 
    public static void sendNoticeToAdmin(Exception ex, String mdc, String requestUrl) { 
 | 
  
 | 
        String simpleMsg = ex.getMessage(); 
 | 
  
 | 
        if (!EXCLUDE_EXCEPTION.contains(simpleMsg)) { 
 | 
  
 | 
  
 | 
            String isOpenDingdingExceptionNotice = PropertiesUtil.getString("is_open_exception_report"); 
 | 
            String showExcptionUrl = PropertiesUtil.getString("showExcptionUrl"); 
 | 
  
 | 
  
 | 
            if (isOpenDingdingExceptionNotice != null && TRUE.equals(isOpenDingdingExceptionNotice)) { 
 | 
  
 | 
                if (StringUtils.isNotBlank(simpleMsg) && simpleMsg.length() > 100) { 
 | 
                    simpleMsg = simpleMsg.substring(0, 100); 
 | 
                } 
 | 
                ProjException p = new ProjException(); 
 | 
                p.setCreateTime(new Date()); 
 | 
                p.setMachine(ComputerTools.getHostName()); 
 | 
                p.setErrorMsg(printStackTraceToString(ex)); 
 | 
                p.setSimpleMsg(simpleMsg); 
 | 
                p.setMdc(mdc); 
 | 
                if(requestUrl!=null){ 
 | 
                    p.setCause(requestUrl); 
 | 
                    SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); 
 | 
                    if (user != null) { 
 | 
                        p.setOwner("异常操作人:" + user.getSuName() + "账号:" + user.getSuAccount()); 
 | 
                    } 
 | 
                } 
 | 
  
 | 
                ProjExceptionDao projExceptionDao = WebUtil.getApplicationContext().getBean(ProjExceptionDao.class); 
 | 
                projExceptionDao.insert(p); 
 | 
  
 | 
                LogUtil.info("上报异常通知"); 
 | 
                String title = "hive在服务器:" + ComputerTools.getHostName() + "发生异常"; 
 | 
                String content = "hive异常类型:" + ex.getClass().getName() + "  描述:" + simpleMsg; 
 | 
                DingDingRobotUtil.sendLink("https://oapi.dingtalk.com/robot/send?access_token=62bb902f0e3945f0ece31306b99abae043fc69a66da0ef04d89fd20bf58d88d8", content, title, "", "" + showExcptionUrl + "?id=" + p.getId()); 
 | 
  
 | 
  
 | 
            } 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 获取异常堆栈信息 
 | 
     * 
 | 
     * @param t 
 | 
     * @return 
 | 
     * @author JIANGYOUYAO 
 | 
     * @email 935090232@qq.com 
 | 
     * @date 2018年5月9日 
 | 
     */ 
 | 
    public static String printStackTraceToString(Throwable t) { 
 | 
        StringWriter sw = new StringWriter(); 
 | 
        t.printStackTrace(new PrintWriter(sw, true)); 
 | 
        return sw.getBuffer().toString(); 
 | 
    } 
 | 
  
 | 
} 
 |