Skip to content

Commit 05776e2

Browse files
committed
Support exception mapper extension
1 parent a131257 commit 05776e2

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/BaseRestProtocolServer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626

2727
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
28+
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY;
2829
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
2930

3031
public abstract class BaseRestProtocolServer implements RestProtocolServer {
@@ -38,8 +39,8 @@ public void start(URL url) {
3839
getDeployment().getMediaTypeMappings().put("json", "application/json");
3940
getDeployment().getMediaTypeMappings().put("xml", "text/xml");
4041
getDeployment().getProviderClasses().add(RpcContextFilter.class.getName());
41-
// TODO users can override this mapper, but we just rely on the current priority strategy of resteasy
42-
getDeployment().getProviderClasses().add(RpcExceptionMapper.class.getName());
42+
43+
loadProviders(url.getParameter(EXCEPTION_MAPPER_KEY, RpcExceptionMapper.class.getName()));
4344

4445
loadProviders(url.getParameter(EXTENSION_KEY, ""));
4546

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/Constants.java

+3
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ public interface Constants {
3535
String TOMCAT = "tomcat";
3636

3737
String NETTY = "netty";
38+
39+
// exception mapper
40+
String EXCEPTION_MAPPER_KEY = "exception.mapper";
3841
}

dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,21 @@ public class RpcExceptionMapper implements ExceptionMapper<RpcException> {
2828

2929
@Override
3030
public Response toResponse(RpcException e) {
31-
// TODO do more sophisticated exception handling and output
3231
if (e.getCause() instanceof ConstraintViolationException) {
3332
return handleConstraintViolationException((ConstraintViolationException) e.getCause());
3433
}
3534
// we may want to avoid exposing the dubbo exception details to certain clients
36-
// TODO for now just do plain text output
3735
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal server error: " + e.getMessage()).type(ContentType.TEXT_PLAIN_UTF_8).build();
3836
}
3937

4038
protected Response handleConstraintViolationException(ConstraintViolationException cve) {
4139
ViolationReport report = new ViolationReport();
4240
for (ConstraintViolation<?> cv : cve.getConstraintViolations()) {
4341
report.addConstraintViolation(new RestConstraintViolation(
44-
cv.getPropertyPath().toString(),
45-
cv.getMessage(),
46-
cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
42+
cv.getPropertyPath().toString(),
43+
cv.getMessage(),
44+
cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
4745
}
48-
// TODO for now just do xml output
4946
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build();
5047
}
5148
}

dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@
3838
import org.junit.jupiter.api.Assertions;
3939
import org.junit.jupiter.api.Test;
4040

41+
import javax.ws.rs.core.Response;
42+
import javax.ws.rs.ext.ExceptionMapper;
4143
import java.util.Map;
4244

4345
import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
46+
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY;
4447
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
4548
import static org.hamcrest.CoreMatchers.equalTo;
4649
import static org.hamcrest.CoreMatchers.is;
4750
import static org.hamcrest.CoreMatchers.nullValue;
4851
import static org.hamcrest.MatcherAssert.assertThat;
4952

5053
class RestProtocolTest {
51-
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
52-
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
54+
private final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
55+
private final ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
5356
private final int availablePort = NetUtils.getAvailablePort();
5457
private final URL exportUrl = URL.valueOf("rest://127.0.0.1:" + availablePort + "/rest?interface=org.apache.dubbo.rpc.protocol.rest.DemoService");
5558
private final ModuleServiceRepository repository = ApplicationModel.defaultModel().getDefaultModule().getServiceRepository();
@@ -246,6 +249,29 @@ void testDefaultPort() {
246249
assertThat(protocol.getDefaultPort(), is(80));
247250
}
248251

252+
@Test
253+
void testExceptionMapper() {
254+
DemoService server = new DemoServiceImpl();
255+
256+
URL url = this.registerProvider(exportUrl, server, DemoService.class);
257+
258+
URL exceptionUrl = url.addParameter(EXCEPTION_MAPPER_KEY, TestExceptionMapper.class.getName());
259+
260+
protocol.export(proxy.getInvoker(server, DemoService.class, exceptionUrl));
261+
262+
DemoService referDemoService = this.proxy.getProxy(protocol.refer(DemoService.class, exceptionUrl));
263+
264+
Assertions.assertEquals("test-exception", referDemoService.error());
265+
}
266+
267+
public static class TestExceptionMapper implements ExceptionMapper<RuntimeException> {
268+
269+
@Override
270+
public Response toResponse(RuntimeException e) {
271+
return Response.ok("test-exception").build();
272+
}
273+
}
274+
249275
private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) {
250276
ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
251277
ProviderModel providerModel = new ProviderModel(

0 commit comments

Comments
 (0)