apache / apache/dubbo

[Bug] How to get attachment data in the consumer ,attachment data set from provider cusome exception filter?

Open
#14,416 8 comments 0 reactions 0 assignees View on GitHub
component/need-triage type/need-triage
Dominant language
Java
Stars
41.6k
Forks
26.4k
Avg merge
15h 13m
Merged PRs (30d)
4

Description

### Pre-check

- [X] I am sure that all the content I provide is in English.

### Search before asking

- [X] I had searched in the [issues](https://github.com/apache/dubbo/issues?q=is%3Aissue) and found no similar issues.

### Apache Dubbo Component

Java SDK (apache/dubbo)

### Dubbo Version

3.2.14

### Steps to reproduce this issue

How to get attachment data in the consumer ,attachment data set from provider cusome exception filter ?
DubboProviderExceptionFilter:
RpcContextAttachment serverContext = RpcContext.getServerContext();
serverContext.setObjectAttachment(ATTA_NAME_EXCEPTION_CODE, br.getCode());
DubboConsumerExceptionFilter:
RpcContext.getServerContext().getObjectAttachments() ,RpcContext.getContext().getAttachments();
Neither method can be obtained

### What you expected to happen

DubboProviderExceptionFilter set attachment data , DubboConsumerExceptionFilter get server attachment data ,used to customize exceptions extra data

### Anything else

source code:
```
@Activate(group = CommonConstants.PROVIDER )
public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
public static String ATTA_NAME_EXCEPTION_CODE = "__code";
public static String ATTA_NAME_EXCEPTION_DETAIL_MSG = "__detailMsg";
public static String ATTA_NAME_EXCEPTION_DETAIL_DATA = "__data";

@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();

String className = exception.getClass().getName();
//如果是我们系统自定义异常直接返回
if ( ClassUtil2.isAssignable(ServiceException.class, exception.getClass())) {
//服务提供方写入回传参数
ServiceException br = (ServiceException) exception;
invocation.setObjectAttachment(ATTA_NAME_EXCEPTION_CODE, br.getCode());
invocation.setObjectAttachment(ATTA_NAME_EXCEPTION_DETAIL_MSG, br.getDetailMsg());
invocation.setObjectAttachment(ATTA_NAME_EXCEPTION_DETAIL_DATA, br.getExtraData());

RpcContextAttachment serverContext = RpcContext.getServerContext();
serverContext.setObjectAttachment(ATTA_NAME_EXCEPTION_CODE, br.getCode());
serverContext.setObjectAttachment(ATTA_NAME_EXCEPTION_DETAIL_MSG, br.getDetailMsg());
serverContext.setObjectAttachment(ATTA_NAME_EXCEPTION_DETAIL_DATA, br.getExtraData());

return;
}

```
```
@Activate(group = CommonConstants.CONSUMER )
public class DubboConsumerExceptionFilter implements Filter, Filter.Listener {

@Override
public void onResponse(Result appResponse, Invoker invoker, Invocation invocation) {
Throwable exec = appResponse.getException();
if (exec != null && ServiceException.class.isAssignableFrom(exec.getClass())) {
Map attachments = invocation.getAttachments();

if (attachments.containsKey(ATTA_NAME_EXCEPTION_CODE)) {
ReflectionUtil.setFieldValue(exec, "code", attachments.get(ATTA_NAME_EXCEPTION_CODE));
}

if (attachments.containsKey(ATTA_NAME_EXCEPTION_DETAIL_MSG)) {
ReflectionUtil.setFieldValue(exec, "detailMsg", attachments.get(ATTA_NAME_EXCEPTION_DETAIL_MSG));
}

if (attachments.containsKey(ATTA_NAME_EXCEPTION_DETAIL_DATA)) {
ReflectionUtil.setFieldValue(exec, "extraData", attachments.get(ATTA_NAME_EXCEPTION_DETAIL_DATA));
}

}
}

```

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

- [X] Yes I am willing to submit a pull request on my own!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct)

Contributor guide

Open the contributing guide

Research direction

Start by tracing the mentioned DubboProviderExceptionFilter and DubboConsumerExceptionFilter entry points, then compare invocation attachments with RpcContext server and consumer attachments during exception handling. Verify where provider-side data is carried across the RPC response and confirm completion by making the consumer receive the custom exception code, detail message, and extra data.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.