|
|
|
|
@ -15,7 +15,8 @@ import javax.validation.ConstraintViolation;
|
|
|
|
|
import javax.validation.ConstraintViolationException;
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
|
|
|
|
|
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.*;
|
|
|
|
|
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
|
|
|
|
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
|
|
|
|
|
|
|
|
|
@Activate(group = CommonConstants.PROVIDER)
|
|
|
|
|
public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
|
|
|
|
|
@ -31,30 +32,24 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
|
|
|
|
|
public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
|
|
|
|
|
if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
|
|
|
|
|
try {
|
|
|
|
|
// 转换异常
|
|
|
|
|
// 1. 转换异常
|
|
|
|
|
Throwable exception = appResponse.getException();
|
|
|
|
|
// 1. 参数校验异常
|
|
|
|
|
// 1.1 参数校验异常
|
|
|
|
|
if (exception instanceof ConstraintViolationException) {
|
|
|
|
|
exception = this.constraintViolationExceptionHandler((ConstraintViolationException) exception);
|
|
|
|
|
// 2. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
|
|
|
|
|
// 1. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
|
|
|
|
|
} else if (exception instanceof ServiceException) {
|
|
|
|
|
// 3. 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
|
|
|
|
|
// 1.3 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
|
|
|
|
|
} else {
|
|
|
|
|
exception = this.defaultExceptionHandler(exception, invocation);
|
|
|
|
|
assert exception != null;
|
|
|
|
|
}
|
|
|
|
|
// 根据不同的方法 schema 返回结果
|
|
|
|
|
// 第一种情况,返回参数类型是 CommonResult 的情况,则将 ServiceException 转换成 CommonResult
|
|
|
|
|
if (isReturnCommonResult(invocation)) {
|
|
|
|
|
// 清空异常
|
|
|
|
|
appResponse.setException(null);
|
|
|
|
|
// 设置结果
|
|
|
|
|
if (exception instanceof ServiceException) {
|
|
|
|
|
appResponse.setValue(CommonResult.error((ServiceException) exception));
|
|
|
|
|
} else {
|
|
|
|
|
appResponse.setValue(CommonResult.error((GlobalException) exception));
|
|
|
|
|
}
|
|
|
|
|
// 第二种情况,未包装成 CommonResult 的情况,则直接抛出 ServiceException 异常
|
|
|
|
|
// 2. 根据不同的方法 schema 返回结果
|
|
|
|
|
// 2.1 如果是 ServiceException 异常,并且返回参数类型是 CommonResult 的情况,则将转换成 CommonResult 返回
|
|
|
|
|
if (isReturnCommonResult(invocation) && exception instanceof ServiceException) {
|
|
|
|
|
appResponse.setException(null); // 一定要清空异常
|
|
|
|
|
appResponse.setValue(CommonResult.error((ServiceException) exception));
|
|
|
|
|
// 2.2 如果是 GlobalException 全局异常,则直接抛出
|
|
|
|
|
} else {
|
|
|
|
|
appResponse.setException(exception);
|
|
|
|
|
}
|
|
|
|
|
|