ExceptionAspect-异常处理

ExceptionAspect

package com.mozhu.common.aspect.exception;

import com.mozhu.common.bean.JsonResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 切面
 * 处理异常及日志
 */
@Aspect
@Component
public class ExceptionAspect {

    public static Logger logger = LoggerFactory.getLogger(ExceptionAspect.class);

    private final String loginUrl = "";
    /**
     * 切点,切所有业务层及接口
     * execution(* com.mozhu.bssh..*.*Service.* (..)) or execution(* com.mozhu.bssh..*.*Controller.* (..))
     */
    private final static String CUT_POINT = "execution(* com.mozhu.bst..*.*Controller.* (..)) or execution(* com.mozhu.bst.system.service.impl.SysUserServiceImpl.* (..))";

    @Around(value = CUT_POINT)
    public Object exceptionAspect(ProceedingJoinPoint target) {
        try {
            Object obj = target.proceed();
            return obj;
        } catch (Throwable e) {
            // 业务异常
            if (e instanceof BusinessException) {
                logger.warn(e.getMessage());
                // 非登录异常,传警告异常到前端
                return new JsonResult<Object>(JsonResult.WARNING, e.getMessage());
            } else {
                // 未知异常,传错误异常到前端
                GlobalException globalException = new GlobalException(e);
                logger.error("ExceptionId:{}", globalException.getExceptionId());
                e.printStackTrace();
                return new JsonResult<Object>(JsonResult.ERROR, "程序执行失败。ExceptionId:" + globalException.getExceptionId());
            }
        }
    }

    /**
     * 获取异常行数
     *
     * @param target
     * @param e
     * @return
     */
    private Integer getErrorLineNumber(ProceedingJoinPoint target, Throwable e) {
        Class<?> cls = target.getTarget().getClass();
        String clsName = cls.getName();
        String methodName = target.getSignature().getName();

        int n = 0;
        for (int i = 0; i < e.getStackTrace().length; i++) {
            StackTraceElement stackTraceElement = e.getStackTrace()[i];
            if (clsName.equals(stackTraceElement.getClassName()) && methodName.equals(stackTraceElement.getMethodName())) {
                n = e.getStackTrace()[i].getLineNumber();
            }
        }
        return n;
    }


    public class RequestMessage {
        private String clsName;
        private String methodName;
        private String requestName;

        public String getClsName() {
            return clsName;
        }

        public void setClsName(String clsName) {
            this.clsName = clsName;
        }

        public String getMethodName() {
            return methodName;
        }

        public void setMethodName(String methodName) {
            this.methodName = methodName;
        }

        public String getRequestName() {
            return requestName;
        }

        public void setRequestName(String requestName) {
            this.requestName = requestName;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值