@Aspect
@Component
@Slf4jpublic classServiceExceptionAspect {
@Around("execution(* com.sankuai.groceryauth.auth.poiquery.service.thrift..*.*(..)) ||" +
"execution(* com.sankuai.groceryauth.auth.poiquery.service.crane..*.*(..))")publicObject 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();returnresult;
}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);returnresult;
}throwe;
}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());returnresult;
}throwe;
}catch(Throwable e) {
log.error("TServiceExceptionAspect catch Throwable, method:{},param:{}", methodFullName, args, e);
doCatLog(methodFullName, e);if (setMsg == null || setCode == null) {throwe;
}else{
Object result=signature.getReturnType().newInstance();
setMsg.invoke(result,"系统内部错误");
setCode.invoke(result, Constants.INTERNAL_ERROR);returnresult;
}
}
}private voiddoCatLog(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, intcode, 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();
}
}
}