apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.54k stars 26.44k forks source link

[Bug] 3.2.13 The provider is unable to throw a custom exception. #14426

Open JusFree opened 4 months ago

JusFree commented 4 months ago

Pre-check

Search before asking

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

3.2.13

Steps to reproduce this issue

Declared a custom interceptor using SPI as follows:

@Activate(group = {CommonConstants.PROVIDER})
public class DubboExceptionFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try {
            Result result = invoker.invoke(invocation);
            if (result.hasException()) {
                Throwable exception = result.getException();
                if (exception instanceof BaseException) {
                    BaseException customException = (BaseException) exception;
                    return AsyncRpcResult.newDefaultAsyncResult(new BaseException(customException.getMessage(), customException.getCode()), invocation);
                }
            }
            return result;
        } catch (RpcException e) {
            throw e;
        }
    }
}

However, the exception returned by the caller is still wrapped in a RuntimeException.

What you expected to happen

Able to correctly retrieve the custom exception.

Anything else

No response

Are you willing to submit a pull request to fix on your own?

Code of Conduct

wcy666103 commented 4 months ago

It looks like someone is trying to solve this problem https://github.com/apache/dubbo/pull/14417

JusFree commented 4 months ago

看起来有人正在尝试解决这个问题#14417

There are some differences. #14417 deals with cross-language exceptions, while my issue concerns custom exceptions between Java services. In the ExceptionFilter, when the same exception is defined within the same directory as the API, it can be thrown directly. However, when the API and the exception class are not in the same package, the exception is uniformly wrapped as a RuntimeException.

wcy666103 commented 4 months ago

看起来有人正在尝试解决这个问题#14417

There are some differences. #14417 deals with cross-language exceptions, while my issue concerns custom exceptions between Java services. In the ExceptionFilter, when the same exception is defined within the same directory as the API, it can be thrown directly. However, when the API and the exception class are not in the same package, the exception is uniformly wrapped as a RuntimeException.

You can submit a pr to try and fix the issue you're having.

incubus-ohy commented 1 week ago

请问后续有什么方案可以解决这个问题吗

JusFree commented 1 week ago

请问后续有什么方案可以解决这个问题吗

目前时放在api目录下,他是可以抛出自定义异常的,之前看了源码如果要不限包抛出自定义异常的话需要自己定义白名单,侵入性有点大就没有去改造了。 路径大概如下: image