fix
Helius
2021-09-07 d659b10032c83129f95d6c756401acbe53b5ff28
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
package com.xcong.excoin.common.aop;
 
import cn.hutool.http.HttpException;
import com.xcong.excoin.common.exception.GlobalException;
import com.xcong.excoin.common.system.bean.SysExceptionDetailEntity;
import com.xcong.excoin.modules.platform.dao.SysExceptionDetailDao;
import com.xcong.excoin.utils.ThreadPoolUtils;
import com.xcong.excoin.utils.dingtalk.DingTalkUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.client.ResourceAccessException;
 
import javax.net.ssl.SSLException;
import javax.validation.ValidationException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @author wzy
 * @date 2021-03-05
 **/
@Slf4j
@Aspect
@Component
public class ExceptionCatchAspect {
 
    private static final List EXCLUDE_EXCEPTION = new ArrayList(Arrays.asList("java.io.IOException: Broken pipe"));
 
    @Autowired
    private SysExceptionDetailDao sysExceptionDetailDao;
 
    @Value("${spring.profiles.active}")
    private String profiles;
 
    @Pointcut("execution(* com.xcong.excoin..*.*(..))")
    public void exceptionCatch() {
    }
 
    @AfterThrowing(pointcut = "exceptionCatch()", throwing = "ex")
    public void afterThrows(JoinPoint jp, Exception ex) throws Exception {
        if (ex instanceof HttpException || ex instanceof ResourceAccessException || ex instanceof GlobalException || ex instanceof MethodArgumentNotValidException || ex instanceof ValidationException || ex instanceof DuplicateKeyException || ex instanceof BadCredentialsException || ex instanceof UsernameNotFoundException || ex instanceof SSLException) {
            throw ex;
        }
 
        if (EXCLUDE_EXCEPTION.contains(ex.getMessage())) {
            throw ex;
        }
 
        SysExceptionDetailEntity exceptionData = new SysExceptionDetailEntity();
        String exStr = printStackTraceToString(ex);
        ThreadPoolUtils.EXECUTOR.execute(new Runnable(){
            @Override
            public void run() {
                try {
                    log.info("插入");
                    exceptionData.setCreateTime(new Date());
                    exceptionData.setMachine(InetAddress.getLocalHost().getHostName() + "-" + profiles);
                    exceptionData.setAddress(InetAddress.getLocalHost().getHostAddress());
                    exceptionData.setExceptionMsg(exStr);
                    exceptionData.setSimpleMsg(ex.getMessage());
                    sysExceptionDetailDao.insert(exceptionData);
 
//                    DingTalkUtils.sendMsg(profiles + "--" + ex.getMessage(), exStr.substring(0, 200), exceptionData.getId());
                } catch (Exception e) {
                    log.error("exception aop", e);
                }
            }
        });
        throw ex;
    }
 
    public String printStackTraceToString(Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw, true));
        return sw.getBuffer().toString();
    }
}