zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    如何选择机器学习算法 转
    机器学习经典算法详解及Python实现--基于SMO的SVM分类器
    机器学习(Machine Learning)&深度学习(Deep Learning)资料
    计算智能在设备状态维护中的应用
    LaTeX 在编译时出现 File ended while scanning use of @writefile错误
    LaTeX 中插入图片使其紧跟插入的文字之后
    LaTeX 制作表格
    LaTeX 中换段落
    LaTeX 中使用三级标题
    使用 WinEdt 来写中文文章or 建模论文
  • 原文地址:https://www.cnblogs.com/honghong75042/p/14254315.html
Copyright © 2011-2022 走看看