jyy
2021-01-25 949efdc17fa833f6511c0e5902057f3be0fe72c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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();
    }
 
}