修改 Dubbo Exception Filter 的处理,针对系统异常,还是直接抛出
							parent
							
								
									c0407267b9
								
							
						
					
					
						commit
						e04c9584e3
					
				| 
						 | 
					@ -15,7 +15,8 @@ import javax.validation.ConstraintViolation;
 | 
				
			||||||
import javax.validation.ConstraintViolationException;
 | 
					import javax.validation.ConstraintViolationException;
 | 
				
			||||||
import java.lang.reflect.Type;
 | 
					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)
 | 
					@Activate(group = CommonConstants.PROVIDER)
 | 
				
			||||||
public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
 | 
					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) {
 | 
					    public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
 | 
				
			||||||
        if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
 | 
					        if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                // 转换异常
 | 
					                // 1. 转换异常
 | 
				
			||||||
                Throwable exception = appResponse.getException();
 | 
					                Throwable exception = appResponse.getException();
 | 
				
			||||||
                // 1. 参数校验异常
 | 
					                // 1.1 参数校验异常
 | 
				
			||||||
                if (exception instanceof ConstraintViolationException) {
 | 
					                if (exception instanceof ConstraintViolationException) {
 | 
				
			||||||
                    exception = this.constraintViolationExceptionHandler((ConstraintViolationException) exception);
 | 
					                    exception = this.constraintViolationExceptionHandler((ConstraintViolationException) exception);
 | 
				
			||||||
                // 2. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
 | 
					                // 1. ServiceException 业务异常,因为不会有序列化问题,所以无需处理
 | 
				
			||||||
                } else if (exception instanceof ServiceException) {
 | 
					                } else if (exception instanceof ServiceException) {
 | 
				
			||||||
                // 3. 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
 | 
					                // 1.3 其它异常,转换成 ServiceException 业务异常,避免可能存在的反序列化问题
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    exception = this.defaultExceptionHandler(exception, invocation);
 | 
					                    exception = this.defaultExceptionHandler(exception, invocation);
 | 
				
			||||||
                    assert exception != null;
 | 
					                    assert exception != null;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                // 根据不同的方法 schema 返回结果
 | 
					                // 2. 根据不同的方法 schema 返回结果
 | 
				
			||||||
                // 第一种情况,返回参数类型是 CommonResult 的情况,则将 ServiceException 转换成 CommonResult
 | 
					                // 2.1 如果是 ServiceException 异常,并且返回参数类型是 CommonResult 的情况,则将转换成 CommonResult 返回
 | 
				
			||||||
                if (isReturnCommonResult(invocation)) {
 | 
					                if (isReturnCommonResult(invocation) && exception instanceof ServiceException) {
 | 
				
			||||||
                    // 清空异常
 | 
					                    appResponse.setException(null); // 一定要清空异常
 | 
				
			||||||
                    appResponse.setException(null);
 | 
					                    appResponse.setValue(CommonResult.error((ServiceException) exception));
 | 
				
			||||||
                    // 设置结果
 | 
					                // 2.2 如果是 GlobalException 全局异常,则直接抛出
 | 
				
			||||||
                    if (exception instanceof ServiceException) {
 | 
					 | 
				
			||||||
                        appResponse.setValue(CommonResult.error((ServiceException) exception));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                        appResponse.setValue(CommonResult.error((GlobalException) exception));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                // 第二种情况,未包装成 CommonResult 的情况,则直接抛出 ServiceException 异常
 | 
					 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    appResponse.setException(exception);
 | 
					                    appResponse.setException(exception);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -160,9 +160,15 @@ public class GlobalExceptionHandler {
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @ExceptionHandler(value = GlobalException.class)
 | 
					    @ExceptionHandler(value = GlobalException.class)
 | 
				
			||||||
    public CommonResult globalExceptionHandler(HttpServletRequest req, GlobalException ex) {
 | 
					    public CommonResult globalExceptionHandler(HttpServletRequest req, GlobalException ex) {
 | 
				
			||||||
        logger.error("[globalExceptionHandler]", ex);
 | 
					        // 系统异常时,才打印异常日志
 | 
				
			||||||
        // 插入异常日志
 | 
					        if (INTERNAL_SERVER_ERROR.getCode().equals(ex.getCode())) {
 | 
				
			||||||
        this.createExceptionLog(req, ex);
 | 
					            logger.error("[globalExceptionHandler]", ex);
 | 
				
			||||||
 | 
					            // 插入异常日志
 | 
				
			||||||
 | 
					            this.createExceptionLog(req, ex);
 | 
				
			||||||
 | 
					        // 普通全局异常,打印 info 日志即可
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            logger.info("[globalExceptionHandler]", ex);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        // 返回 ERROR CommonResult
 | 
					        // 返回 ERROR CommonResult
 | 
				
			||||||
        return CommonResult.error(ex);
 | 
					        return CommonResult.error(ex);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue