Global exception capture fails under aop surround notification

Configuring global exceptions when aop's surround notification exists has no effect, mainly because the exception that will occur in the surround notification is not thrown, resulting in global exceptions that cannot be caught.
The solution is to throw the exception in the aop surround notification again
@Aspect
@Component
public class LogAspect {
    
    

    @Pointcut("@annotation(com.dh.platform.entity.LogAnnotate)")
    public void logPoint(){
    
    

    }

    @Around(value = "logPoint()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        long startTime = 0,endTime = 0;
        TableOperateLog operateLog=finishTableOperateLog();
        String[] methods=joinPoint.getSignature().toLongString().split(" ");
        operateLog.setController(methods[methods.length-1].split("\\(")[0]);
        Object object = null;
        try {
    
    
            startTime=System.currentTimeMillis();
            object=joinPoint.proceed();
            CommonResponse commonResponse= HttpCommon.jsonToObject((String) object,CommonResponse.class);
            operateLog.setHttpStatusCode(200);
            String msg=null;
            if(commonResponse.getResult()!=1){
    
    
                operateLog.setHttpStatusCode(400);
                if(commonResponse.getResult()==-1){
    
    
                    msg="权限不足。";
                }
                if(commonResponse.getResult()==-88){
    
    
                    msg="登陆超时。";
                }
                if(commonResponse.getResult()>=0){
    
    
                    msg = commonResponse.getReasonInfo();
                }
            }else{
    
    
                msg=commonResponse.getReasonInfo();
            }
            operateLog.setMemo(msg);
            operateLog.setReturnData(JSON.toJSONString(commonResponse.getDatum()));
        } catch (Throwable throwable) {
    
    
            operateLog.setHttpStatusCode(400);
            operateLog.setMemo("接口返回数据异常。");
            // throw throwable  不加这行代码全局异常将失效
            throw throwable;
        }finally {
    
    
            endTime=System.currentTimeMillis();
            operateLog.setReturnTime(Tools.GetNowTime());
            operateLog.setTimeConsuming(Integer.parseInt(String.valueOf(endTime-startTime)));
            commonService.saveTabOperateLog(operateLog);
        }
        return object;
    }

Guess you like

Origin blog.csdn.net/qq_37262094/article/details/103532560