产生该问题的主要原因是Dubbo底层过滤器拦截了异常响应见源码
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.rpc.filter;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.extension.DisableInject;
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.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.lang.reflect.Method;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FILTER_VALIDATION_EXCEPTION;
/**
* ExceptionInvokerFilter
* <p>
* Functions:
* <ol>
* <li>unexpected exception will be logged in ERROR level on provider side. Unexpected exception are unchecked
* exception not declared on the interface</li>
* <li>Wrap the exception not introduced in API package into RuntimeException. Framework will serialize the outer exception but stringnize its cause in order to avoid of possible serialization problem on client side</li>
* </ol>
*/
@Activate(group = CommonConstants.PROVIDER)
public class ExceptionFilter implements Filter, Filter.Listener {
private ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ExceptionFilter.class);
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
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();
// directly throw if it's checked exception
if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
return;
}
// directly throw if the exception appears in the signature
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 e) {
return;
}
// for the exception not found in method's signature, print ERROR message in server's log.
logger.error(
CONFIG_FILTER_VALIDATION_EXCEPTION,
"",
"",
"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);
// 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;
}
// directly throw if it's JDK exception
String className = exception.getClass().getName();
if (className.startsWith("java.")
|| className.startsWith("javax.")
|| className.startsWith("jakarta.")) {
return;
}
// directly throw if it's dubbo exception
if (exception instanceof RpcException) {
return;
}
// otherwise, wrap with RuntimeException and throw back to the client
appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
} catch (Throwable e) {
logger.warn(
CONFIG_FILTER_VALIDATION_EXCEPTION,
"",
"",
"Fail to ExceptionFilter when called by "
+ RpcContext.getServiceContext().getRemoteHost() + ". service: "
+ invoker.getInterface().getName() + ", method: " + RpcUtils.getMethodName(invocation)
+ ", exception: "
+ e.getClass().getName() + ": " + e.getMessage(),
e);
}
}
}
@Override
public void onError(Throwable e, Invoker<?> invoker, Invocation invocation) {
logger.error(
CONFIG_FILTER_VALIDATION_EXCEPTION,
"",
"",
"Got unchecked and undeclared exception which called by "
+ RpcContext.getServiceContext().getRemoteHost() + ". service: "
+ invoker.getInterface().getName() + ", method: " + RpcUtils.getMethodName(invocation)
+ ", exception: "
+ e.getClass().getName() + ": " + e.getMessage(),
e);
}
// For test purpose
@DisableInject
public void mockLogger(ErrorTypeAwareLogger logger) {
this.logger = logger;
}
}
拦截器中只能检测到 java javax jakarta开头的异常或者是 RpcException,否则都会将异常整体转换成字符串作为RuntimeException返回
解决办法
1、如果系统有自定义的RpcException拦截器, 在Dubbo远程调用阶段返回的自定义异常用new RpcException(自定义异常.getMessage())包装一层
2、直接返回new RuntimeException(e.geMessage())
try{
}catch (Exception e){
throw new RuntimeException(e.getMessage());
}
768

被折叠的 条评论
为什么被折叠?



