bom 文件调整
This commit is contained in:
@@ -1,106 +0,0 @@
|
||||
package cn.iocoder.common.framework.dubbo;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import org.apache.dubbo.common.constants.CommonConstants;
|
||||
import org.apache.dubbo.common.extension.Activate;
|
||||
import org.apache.dubbo.common.logger.Logger;
|
||||
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 java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 基于 {@link org.apache.dubbo.rpc.filter.ExceptionFilter} 实现
|
||||
*
|
||||
* 主要目的是,一些全局性的异常,能够返回。因为,Dubbo Consumer 能够保证,一定会引入全局性的异常。
|
||||
*/
|
||||
@Activate(group = CommonConstants.PROVIDER)
|
||||
public class DubboExceptionFilter implements Filter {
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
public DubboExceptionFilter() {
|
||||
this(LoggerFactory.getLogger(ExceptionFilter.class));
|
||||
}
|
||||
|
||||
public DubboExceptionFilter(Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
try {
|
||||
return invoker.invoke(invocation);
|
||||
} catch (RuntimeException e) {
|
||||
logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
|
||||
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
|
||||
+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
|
||||
if (result.hasException() && GenericService.class != invoker.getInterface()) {
|
||||
try {
|
||||
Throwable exception = result.getException();
|
||||
|
||||
// directly throw if it's checked exception
|
||||
if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
|
||||
return result;
|
||||
} else if (exception instanceof ServiceException) { // add by 芋艿。如果是业务异常,继续抛出
|
||||
return result;
|
||||
}
|
||||
// directly throw if the exception appears in the signature
|
||||
try {
|
||||
Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
|
||||
Class<?>[] exceptionClassses = method.getExceptionTypes();
|
||||
for (Class<?> exceptionClass : exceptionClassses) {
|
||||
if (exception.getClass().equals(exceptionClass)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// for the exception not found in method's signature, print ERROR message in server's log.
|
||||
logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
|
||||
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
|
||||
+ ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);
|
||||
|
||||
// directly throw if exception class and interface class are in the same jar file.
|
||||
String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
|
||||
String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
|
||||
if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) {
|
||||
return result;
|
||||
}
|
||||
// directly throw if it's JDK exception
|
||||
String className = exception.getClass().getName();
|
||||
if (className.startsWith("java.") || className.startsWith("javax.")) {
|
||||
return result;
|
||||
}
|
||||
// directly throw if it's dubbo exception
|
||||
if (exception instanceof RpcException) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// otherwise, wrap with RuntimeException and throw back to the client
|
||||
result.setException(new RuntimeException(StringUtils.toString(exception)));
|
||||
return result;
|
||||
} catch (Throwable e) {
|
||||
logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost()
|
||||
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
|
||||
+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,4 +43,6 @@ public final class ServiceException extends RuntimeException {
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,52 +1,9 @@
|
||||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import cn.iocoder.common.framework.exception.ServiceException;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
|
||||
public class ExceptionUtil {
|
||||
|
||||
public static ServiceException getServiceException(Exception e) {
|
||||
if (e instanceof UndeclaredThrowableException) {
|
||||
return getServiceException((UndeclaredThrowableException) e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 处理 Spring 动态代理调用时,发生 UndeclaredThrowableException 的情况。
|
||||
// 不了解的胖友,可以先看看 https://segmentfault.com/a/1190000012262244 文章
|
||||
// 原因是:
|
||||
// 1. Dubbo 动态代理 Wrapper 会将抛出的异常,包装成 InvocationTargetException 异常
|
||||
// 2. Spring AOP 发现是 InvocationTargetException 异常是非方法定义的异常,则会包装成 UndeclaredThrowableException 异常。
|
||||
@Deprecated // https://github.com/apache/incubator-dubbo/issues/3386 Dubbo 2.6.5 会触发该问题,在 2.7.1 版本已经解决。
|
||||
public static ServiceException getServiceException(UndeclaredThrowableException e) {
|
||||
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
|
||||
if (undeclaredThrowable instanceof InvocationTargetException) {
|
||||
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
|
||||
Throwable targetException = invocationTargetException.getTargetException();
|
||||
if (targetException != null & targetException instanceof ServiceException) {
|
||||
return (ServiceException) targetException;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Deprecated // https://github.com/apache/incubator-dubbo/issues/3386 Dubbo 2.6.5 会触发该问题,在 2.7.1 版本已经解决。
|
||||
public static ConstraintViolationException getConstraintViolationException(UndeclaredThrowableException e) {
|
||||
Throwable undeclaredThrowable = e.getUndeclaredThrowable();
|
||||
if (undeclaredThrowable instanceof InvocationTargetException) {
|
||||
InvocationTargetException invocationTargetException = (InvocationTargetException) undeclaredThrowable;
|
||||
Throwable targetException = invocationTargetException.getTargetException();
|
||||
if (targetException instanceof ConstraintViolationException) {
|
||||
return (ConstraintViolationException) targetException;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getMessage(Throwable th) {
|
||||
return ExceptionUtils.getMessage(th);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package cn.iocoder.common.framework.vo;
|
||||
|
||||
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -95,12 +95,12 @@ public final class CommonResult<T> implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@JSONField(serialize = false) // 避免序列化
|
||||
public boolean isSuccess() {
|
||||
return CODE_SUCCESS.equals(code);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@JSONField(serialize = false) // 避免序列化
|
||||
public boolean isError() {
|
||||
return !isSuccess();
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
dubboExceptionFilter=cn.iocoder.common.framework.dubbo.DubboExceptionFilter
|
||||
Reference in New Issue
Block a user