This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 6350b907e5 feat: self define exception (#10667)
6350b907e5 is described below
commit 6350b907e5c7488e85e0204240905b57eb18ef88
Author: aamingaa <[email protected]>
AuthorDate: Wed Oct 19 17:44:14 2022 +0800
feat: self define exception (#10667)
---
.../tri/call/AbstractServerCallListener.java | 2 +-
.../protocol/tri/call/UnaryClientCallListener.java | 8 ++-
.../dubbo/rpc/protocol/tri/ExceptionUtilsTest.java | 65 ++++++++++++++++++++++
.../dubbo/rpc/protocol/tri/support/IGreeter2.java | 26 +++++++++
.../rpc/protocol/tri/support/IGreeter2Impl.java | 24 ++++++++
.../protocol/tri/support/IGreeterException.java | 30 ++++++++++
6 files changed, 152 insertions(+), 3 deletions(-)
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCallListener.java
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCallListener.java
index 7d9c422d05..3dc8d46ca9 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCallListener.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCallListener.java
@@ -66,7 +66,7 @@ public abstract class AbstractServerCallListener implements
AbstractServerCall.L
return;
}
if (response.hasException()) {
- responseObserver.onError(response.getException());
+ onReturn(response.getException());
return;
}
final long cost = System.currentTimeMillis() - stInMillis;
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/UnaryClientCallListener.java
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/UnaryClientCallListener.java
index fb3717b65e..2f00c15ac1 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/UnaryClientCallListener.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/UnaryClientCallListener.java
@@ -42,8 +42,12 @@ public class UnaryClientCallListener implements
ClientCall.Listener {
AppResponse result = new AppResponse();
result.setObjectAttachments(trailers);
if (status.isOk()) {
- result.setValue(appResponse);
- } else {
+ if (appResponse instanceof Exception) {
+ result.setException((Exception) appResponse);
+ } else {
+ result.setValue(appResponse);
+ }
+ } else {
result.setException(status.asException());
}
future.received(status, result);
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/ExceptionUtilsTest.java
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/ExceptionUtilsTest.java
index d0c95eab77..74c0ba63a2 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/ExceptionUtilsTest.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/ExceptionUtilsTest.java
@@ -17,6 +17,17 @@
package org.apache.dubbo.rpc.protocol.tri;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.ClassUtils;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.model.*;
+import org.apache.dubbo.rpc.protocol.tri.support.IGreeter2;
+import org.apache.dubbo.rpc.protocol.tri.support.IGreeter2Impl;
+import org.apache.dubbo.rpc.protocol.tri.support.IGreeterException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -70,4 +81,58 @@ class ExceptionUtilsTest {
List<String> stackFrameList =
ExceptionUtils.getStackFrameList(exception);
Assertions.assertNotEquals(10,stackFrameList.size());
}
+
+ @Test
+ void testSelfDefineException() throws Exception{
+ IGreeter2 serviceImpl = new IGreeter2Impl();
+
+ int availablePort = NetUtils.getAvailablePort();
+ ApplicationModel applicationModel = ApplicationModel.defaultModel();
+
+ URL providerUrl = URL.valueOf(
+ "tri://127.0.0.1:" + availablePort + "/" +
IGreeter2.class.getName());
+
+ ModuleServiceRepository serviceRepository =
applicationModel.getDefaultModule()
+ .getServiceRepository();
+ ServiceDescriptor serviceDescriptor =
serviceRepository.registerService(IGreeter2.class);
+
+ ProviderModel providerModel = new ProviderModel(
+ providerUrl.getServiceKey(),
+ serviceImpl,
+ serviceDescriptor,
+ new ServiceMetadata(), ClassUtils.getClassLoader(IGreeter2.class));
+ serviceRepository.registerProvider(providerModel);
+ providerUrl = providerUrl.setServiceModel(providerModel);
+
+ Protocol protocol = new
TripleProtocol(providerUrl.getOrDefaultFrameworkModel());
+ ProxyFactory proxy =
applicationModel.getExtensionLoader(ProxyFactory.class)
+ .getAdaptiveExtension();
+ Invoker<IGreeter2> invoker = proxy.getInvoker(serviceImpl,
IGreeter2.class, providerUrl);
+ Exporter<IGreeter2> export = protocol.export(invoker);
+
+ URL consumerUrl = URL.valueOf(
+ "tri://127.0.0.1:" + availablePort + "/" +
IGreeter2.class.getName());
+
+ ConsumerModel consumerModel = new
ConsumerModel(consumerUrl.getServiceKey(), null,
+ serviceDescriptor, null,
+ null, null);
+ consumerUrl = consumerUrl.setServiceModel(consumerModel);
+ IGreeter2 greeterProxy =
proxy.getProxy(protocol.refer(IGreeter2.class, consumerUrl));
+ Thread.sleep(1000);
+
+ // 1. test unaryStream
+ String REQUEST_MSG = "hello world";
+ String EXPECT_RESPONSE_MSG = "I am self define exception";
+ try {
+ greeterProxy.echo(REQUEST_MSG);
+ } catch (IGreeterException e) {
+ Assertions.assertEquals(EXPECT_RESPONSE_MSG, e.getMessage());
+ }
+ export.unexport();
+ protocol.destroy();
+ // resource recycle.
+ serviceRepository.destroy();
+ System.out.println("serviceRepository destroyed");
+
+ }
}
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2.java
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2.java
new file mode 100644
index 0000000000..f02b0571b7
--- /dev/null
+++
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2.java
@@ -0,0 +1,26 @@
+/*
+ * 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.protocol.tri.support;
+
+public interface IGreeter2 {
+ String SERVER_MSG = "HELLO WORLD";
+
+ /**
+ * Use request to respond
+ */
+ String echo(String request) throws IGreeterException;
+}
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2Impl.java
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2Impl.java
new file mode 100644
index 0000000000..6dca2c467b
--- /dev/null
+++
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeter2Impl.java
@@ -0,0 +1,24 @@
+/*
+ * 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.protocol.tri.support;
+
+public class IGreeter2Impl implements IGreeter2{
+ @Override
+ public String echo(String request) throws IGreeterException {
+ throw new IGreeterException("I am self define exception");
+ }
+}
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeterException.java
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeterException.java
new file mode 100644
index 0000000000..a76748fb0a
--- /dev/null
+++
b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/support/IGreeterException.java
@@ -0,0 +1,30 @@
+/*
+ * 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.protocol.tri.support;
+
+public class IGreeterException extends Exception {
+ private String message;
+
+ public IGreeterException(String message){
+ super(message);
+ this.message = message;
+ }
+
+ public IGreeterException(String message, Throwable cause) {
+ super(message, cause);
+ }
+}