@ -1,28 +1,25 @@
package cn.iocoder. mall.dubbo.core.filter ;
package cn.iocoder. yudao.framework.rpc.core.dubbo ;
import cn.iocoder.common.framework.exception.GlobalException ;
import cn.iocoder.common.framework.exception.ServiceException ;
import cn.iocoder.common.framework.util.ExceptionUtil ;
import cn.iocoder.common.framework.vo.CommonResult ;
import cn.hutool.core.exceptions.ExceptionUtil ;
import cn.iocoder.yudao.framework.common.exception.ServerException ;
import cn.iocoder.yudao.framework.common.exception.ServiceException ;
import cn.iocoder.yudao.framework.common.pojo.CommonResult ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.dubbo.common.constants.CommonConstants ;
import org.apache.dubbo.common.extension.Activate ;
import org.apache.dubbo.rpc.* ;
import org.apache.dubbo.rpc.service.GenericService ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import javax.validation.ConstraintViolation ;
import javax.validation.ConstraintViolationException ;
import java.lang.reflect.Type ;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST ;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR ;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.* ;
@Activate ( group = CommonConstants . PROVIDER ) // TODO 优化点:设置下顺序
@Slf4j
public class DubboProviderExceptionFilter implements Filter , Filter . Listener {
private Logger logger = LoggerFactory . getLogger ( DubboProviderExceptionFilter . class ) ;
@Override
public Result invoke ( Invoker < ? > invoker , Invocation invocation ) throws RpcException {
return invoker . invoke ( invocation ) ;
@ -55,14 +52,14 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
appResponse . setException ( exception ) ;
}
} catch ( Throwable e ) {
log ger . warn ( "Fail to ExceptionFilter when called by " + RpcContext . getContext ( ) . getRemoteHost ( ) + ". service: " + invoker . getInterface ( ) . getName ( ) + ", method: " + invocation . getMethodName ( ) + ", exception: " + e . getClass ( ) . getName ( ) + ": " + e . getMessage ( ) , e ) ;
log . warn ( "Fail to ExceptionFilter when called by " + RpcContext . getContext ( ) . getRemoteHost ( ) + ". service: " + invoker . getInterface ( ) . getName ( ) + ", method: " + invocation . getMethodName ( ) + ", exception: " + e . getClass ( ) . getName ( ) + ": " + e . getMessage ( ) , e ) ;
}
}
}
@Override
public void onError ( Throwable e , Invoker < ? > invoker , Invocation invocation ) {
log ger . 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 ) ;
log . 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 ) ;
}
private boolean isReturnCommonResult ( Invocation invocation ) {
@ -78,32 +75,31 @@ public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
if ( ! ( returnType instanceof Class ) ) {
return false ;
}
Class returnClass = ( Class ) returnType ;
Class < ? > returnClass = ( Class < ? > ) returnType ;
return returnClass = = CommonResult . class ;
}
/ * *
* 处 理 Validator 校 验 不 通 过 产 生 的 异 常
* /
private Global Exception constraintViolationExceptionHandler ( ConstraintViolationException ex ) {
log ger . warn ( "[constraintViolationExceptionHandler]" , ex ) ;
private Service Exception constraintViolationExceptionHandler ( ConstraintViolationException ex ) {
log . warn ( "[constraintViolationExceptionHandler]" , ex ) ;
ConstraintViolation < ? > constraintViolation = ex . getConstraintViolations ( ) . iterator ( ) . next ( ) ;
return new Global Exception( BAD_REQUEST . getCode ( ) ,
return new Service Exception( BAD_REQUEST . getCode ( ) ,
String . format ( "请求参数不正确:%s" , constraintViolation . getMessage ( ) ) ) ;
}
/ * *
* 处 理 系 统 异 常 , 兜 底 处 理 所 有 的 一 切
* /
private Global Exception defaultExceptionHandler ( Throwable exception , Invocation invocation ) {
log ger . error ( "[defaultExceptionHandler][service({}) method({}) params({}) 执行异常]" ,
private Server Exception defaultExceptionHandler ( Throwable exception , Invocation invocation ) {
log . error ( "[defaultExceptionHandler][service({}) method({}) params({}) 执行异常]" ,
invocation . getTargetServiceUniqueName ( ) , invocation . getMethodName ( ) , invocation . getArguments ( ) , exception ) ;
// 如果已经是 GlobalException 全局异常,直接返回即可
if ( exception instanceof Global Exception) {
return ( Global Exception) exception ;
if ( exception instanceof Server Exception) {
return ( Server Exception) exception ;
}
return new GlobalException ( INTERNAL_SERVER_ERROR )
. setDetailMessage ( this . buildDetailMessage ( exception , invocation ) ) ;
return new ServerException ( INTERNAL_SERVER_ERROR ) . setMessage ( this . buildDetailMessage ( exception , invocation ) ) ;
}
private String buildDetailMessage ( Throwable exception , Invocation invocation ) {