zainali5120
2021-04-22 d1d773cffb46cfa42a3ba9011eabbd42ddaffdb2
src/main/java/com/xcong/excoin/common/aop/ExceptionCatchAspect.java
@@ -1,19 +1,30 @@
package com.xcong.excoin.common.aop;
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.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 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
@@ -24,8 +35,13 @@
@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() {
@@ -33,13 +49,23 @@
    @AfterThrowing(pointcut = "exceptionCatch()", throwing = "ex")
    public void afterThrows(JoinPoint jp, Exception ex) throws Exception {
        if (ex instanceof GlobalException || ex instanceof MethodArgumentNotValidException || ex instanceof ValidationException || ex instanceof DuplicateKeyException || ex instanceof BadCredentialsException || ex instanceof UsernameNotFoundException) {
            throw ex;
        }
        if (EXCLUDE_EXCEPTION.contains(ex.getMessage())) {
            throw ex;
        }
        SysExceptionDetailEntity exceptionData = new SysExceptionDetailEntity();
        exceptionData.setCreateTime(new Date());
        exceptionData.setMachine(InetAddress.getLocalHost().getHostName());
        exceptionData.setMachine(InetAddress.getLocalHost().getHostName()+"-"+profiles);
        exceptionData.setAddress(InetAddress.getLocalHost().getHostAddress());
        exceptionData.setExceptionMsg(printStackTraceToString(ex.fillInStackTrace()));
        exceptionData.setExceptionMsg(printStackTraceToString(ex));
        exceptionData.setSimpleMsg(ex.getMessage());
        sysExceptionDetailDao.insert(exceptionData);
        DingTalkUtils.sendActionCard(6);
        throw ex;
    }