Java 输出本地方法metho


@Aspect
@Component
@Slf4j
public class ServiceExceptionAspect {

    @Around("execution(* com.sankuai.groceryauth.auth.poiquery.service.thrift..*.*(..)) || " +
            "execution(* com.sankuai.groceryauth.auth.poiquery.service.crane..*.*(..))")
    public Object doAop(ProceedingJoinPoint pjp) throws Throwable {
        String targetClassName = pjp.getTarget().getClass().getSimpleName();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        String methodName = signature.getName();
        String methodFullName = targetClassName + "." + methodName;
        Object[] args = pjp.getArgs();

        log.info("method called:{},param:{}", methodFullName, args);

        Method setCode = Arrays.stream(signature.getReturnType().getMethods())
                .filter(method -> "setCode".equalsIgnoreCase(method.getName()))
                .findFirst()
                .orElse(null);

        Method setMsg = Arrays.stream(signature.getReturnType().getMethods())
                .filter(method -> "setMsg".equalsIgnoreCase(method.getName()))
                .findFirst()
                .orElse(null);

        if (setMsg == null) {
            setMsg = Arrays.stream(signature.getReturnType().getMethods())
                    .filter(method -> "setMessage".equalsIgnoreCase(method.getName()))
                    .findFirst()
                    .orElse(null);
        }

        try {
            Object result = pjp.proceed();
            return result;
        } catch (IllegalArgumentException e) {
            if (setMsg != null && setCode != null) {
                Object result = signature.getReturnType().newInstance();
                setCode.invoke(result, Constants.BAD_PARAMETERS);
                setMsg.invoke(result, e.getMessage());
                log.info("IllegalArgumentException :", e);
                return result;
            }
            throw e;
        } catch (AuthTException e) {
            int code = e.getCode();
            String message = e.getMessage();
            log.warn("ServiceExceptionAspect catch BusinessException, method:{},param:{}, code:{}, message:{}",
                    methodFullName, args, code, message, e);

            doCatLog(methodFullName, e);
            if (setMsg != null && setCode != null) {
                Object result = signature.getReturnType().newInstance();
                setMsg.invoke(result, e.getMessage());
                setCode.invoke(result, e.getCode());
                return result;
            }
            throw e;
        } catch (Throwable e) {
            log.error("TServiceExceptionAspect catch Throwable, method:{},param:{}", methodFullName, args, e);
            doCatLog(methodFullName, e);
            if (setMsg == null || setCode == null) {
                throw e;
            } else {
                Object result = signature.getReturnType().newInstance();
                setMsg.invoke(result, "系统内部错误");
                setCode.invoke(result, Constants.INTERNAL_ERROR);
                return result;
            }
        }
    }

    private void doCatLog(String methodFullName, Throwable e) {
        if (e instanceof AuthTException) {
            AuthTException ex = (AuthTException) e;
            int code = ex.getCode();
            String message = ex.getMessage();
            logBusinessException(methodFullName, ex, code, message);
        } else {
            Cat.logEvent("SystemError", methodFullName);
            Cat.logErrorWithCategory("SystemError-" + methodFullName, "traceId:" + Tracer.id(), e);
            MetricHelper.build().name("SystemError").tag("method", methodFullName).count();
        }
    }

    private void logBusinessException(String methodFullName, Exception ex, int code, String message) {
        Cat.logEvent("BusinessException-" + code, methodFullName);
        MetricHelper.build().name("BusinessException-" + code).tag("method", methodFullName).count();

        if (code == Constants.GATEWAY_ERROR) {
            Cat.logEvent("GatewayError", methodFullName);
            Cat.logErrorWithCategory("GatewayError-" + methodFullName, "traceId:" + Tracer.id() + "." + message, ex);
            MetricHelper.build().name("GatewayError").tag("method", methodFullName).count();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值