一、定义一个rpc异常去继承自己自定义捕获的异常
public class RpcRuntimeException extends BadRequestException implements Serializable {
public RpcRuntimeException(String message) {
super(message);
}
public RpcRuntimeException(Integer code, String message) {
super(code, message);
}
}
二、编写handler
import com.company.pay.core.enumerate.GlobalCallbackEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class RpcRuntimeExceptionHandler {
@AfterThrowing(throwing="exception",pointcut="execution(* com.company.pay.core.api.inf.*.*(..))")
public void afterThrow(Throwable exception){
if(exception instanceof RpcRuntimeException){
log.error(exception.getMessage());
throw new RpcRuntimeException(((RpcRuntimeException) exception).getValue(),((RpcRuntimeException) exception).getIntro());
}else {
throw new RpcRuntimeException(GlobalCallbackEnum.BAD_REQUEST.getValue(),exception.getMessage());
}
}
}