|
@@ -0,0 +1,98 @@
|
|
|
|
|
+package com.yr.warehouse.admin.web.filter;
|
|
|
|
|
+
|
|
|
|
|
+import com.yr.bluecat.common.entity.request.AbstractRequest;
|
|
|
|
|
+import org.apache.dubbo.common.extension.Activate;
|
|
|
|
|
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
|
|
|
|
|
+import org.apache.dubbo.common.logger.LoggerFactory;
|
|
|
|
|
+import org.apache.dubbo.common.utils.ReflectUtils;
|
|
|
|
|
+import org.apache.dubbo.common.utils.StringUtils;
|
|
|
|
|
+import org.apache.dubbo.rpc.*;
|
|
|
|
|
+import org.apache.dubbo.rpc.filter.ExceptionFilter;
|
|
|
|
|
+import org.apache.dubbo.rpc.service.GenericService;
|
|
|
|
|
+import org.apache.dubbo.rpc.support.RpcUtils;
|
|
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 重新dubbo过滤器
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description
|
|
|
|
|
+ * @Author pete
|
|
|
|
|
+ * @Date 2024/2/22 15:53
|
|
|
|
|
+ **/
|
|
|
|
|
+@Activate(
|
|
|
|
|
+ group = {"provider"}
|
|
|
|
|
+)
|
|
|
|
|
+public class DubboExceptionFilter extends ExceptionFilter {
|
|
|
|
|
+
|
|
|
|
|
+ private final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DubboExceptionFilter.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Validated
|
|
|
|
|
+ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
|
|
|
|
+ Class<?> anInterface = invoker.getInterface();
|
|
|
|
|
+ if (anInterface.getName().contains("com.yr.warehouse")) {
|
|
|
|
|
+ Object[] args = invocation.getArguments();
|
|
|
|
|
+ boolean hasAppId = false;
|
|
|
|
|
+ for (Object arg : args) {
|
|
|
|
|
+ // 这个request要放到统一的包里
|
|
|
|
|
+ if (arg instanceof AbstractRequest) {
|
|
|
|
|
+ if (StringUtils.isBlank(((AbstractRequest) arg).getAppId())) {
|
|
|
|
|
+ // throw new MessageException("appId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ hasAppId = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!hasAppId) {
|
|
|
|
|
+ // throw new MessageException("appId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return invoker.invoke(invocation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
|
|
|
|
|
+ if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Throwable exception = appResponse.getException();
|
|
|
|
|
+ if (!(exception instanceof RuntimeException) && exception instanceof Exception) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ Method method = invoker.getInterface().getMethod(RpcUtils.getMethodName(invocation), invocation.getParameterTypes());
|
|
|
|
|
+ Class<?>[] exceptionClasses = method.getExceptionTypes();
|
|
|
|
|
+
|
|
|
|
|
+ for (Class<?> exceptionClass : exceptionClasses) {
|
|
|
|
|
+ if (exception.getClass().equals(exceptionClass)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (NoSuchMethodException var11) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.logger.error("5-36", "", "", "Got unchecked and undeclared exception which called by " + RpcContext.getServiceContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + RpcUtils.getMethodName(invocation) + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);
|
|
|
|
|
+ String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
|
|
|
|
|
+ String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
|
|
|
|
|
+ if (serviceFile != null && exceptionFile != null && !serviceFile.equals(exceptionFile)) {
|
|
|
|
|
+ String className = exception.getClass().getName();
|
|
|
|
|
+ // 自定义异常类,不要转运行时异常
|
|
|
|
|
+ if (!className.startsWith("java.") && !className.startsWith("javax.") && !className.startsWith("jakarta.") && !className.startsWith("com.yr.bluecat.common.entity.exception.")) {
|
|
|
|
|
+ if (exception instanceof RpcException) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Throwable throwable) {
|
|
|
|
|
+ this.logger.warn("5-36", "", "", "Fail to ExceptionFilter when called by " + RpcContext.getServiceContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + RpcUtils.getMethodName(invocation) + ", exception: " + throwable.getClass().getName() + ": " + throwable.getMessage(), throwable);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|