You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/02 10:44:51 UTC
[GitHub] WillemJiang closed pull request #471: [JAV-575] Exception to response converter
WillemJiang closed pull request #471: [JAV-575] Exception to response converter
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/471
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/core/src/test/java/io/servicecomb/core/definition/schema/TestProducerSchemaFactory.java b/core/src/test/java/io/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
index 84bf2e472..7a4a681bc 100644
--- a/core/src/test/java/io/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
+++ b/core/src/test/java/io/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
@@ -110,14 +110,19 @@ public void testGetOrCreateProducer() throws Exception {
Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
ReflectUtils.setField(addBody, "x", 1);
ReflectUtils.setField(addBody, "y", 2);
- Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] {addBody});
+ Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] {addBody}) {
+ @Override
+ public String getInvocationQualifiedName() {
+ return "";
+ }
+ };
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
- invocation = new Invocation((Endpoint) null, operationMeta, new Object[] {1, 2});
+ invocation.setSwaggerArguments(new Object[] {1, 2});
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
diff --git a/samples/custom-handler-sample/custom-handler-consumer/src/main/java/io/servicecomb/samples/customerhandler/handlers/MyHandler.java b/samples/custom-handler-sample/custom-handler-consumer/src/main/java/io/servicecomb/samples/customerhandler/handlers/MyHandler.java
index 6e18981f7..7c9874a02 100644
--- a/samples/custom-handler-sample/custom-handler-consumer/src/main/java/io/servicecomb/samples/customerhandler/handlers/MyHandler.java
+++ b/samples/custom-handler-sample/custom-handler-consumer/src/main/java/io/servicecomb/samples/customerhandler/handlers/MyHandler.java
@@ -19,11 +19,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import io.servicecomb.core.Handler;
import io.servicecomb.core.Invocation;
-import io.servicecomb.core.handler.impl.AbstractHandler;
import io.servicecomb.swagger.invocation.AsyncResponse;
-public class MyHandler extends AbstractHandler {
+public class MyHandler implements Handler {
private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
diff --git a/samples/springmvc-sample/springmvc-consumer/src/main/java/io/servicecomb/samples/springmvc/consumer/SpringmvcConsumerMain.java b/samples/springmvc-sample/springmvc-consumer/src/main/java/io/servicecomb/samples/springmvc/consumer/SpringmvcConsumerMain.java
index 929f38d0d..35fcffe61 100644
--- a/samples/springmvc-sample/springmvc-consumer/src/main/java/io/servicecomb/samples/springmvc/consumer/SpringmvcConsumerMain.java
+++ b/samples/springmvc-sample/springmvc-consumer/src/main/java/io/servicecomb/samples/springmvc/consumer/SpringmvcConsumerMain.java
@@ -59,6 +59,7 @@ public static void main(String[] args) throws Exception {
String metricsResult = restTemplate.getForObject("cse://springmvc/springmvchello/metricsForTest/", String.class);
+ @SuppressWarnings("unchecked")
Map<String, String> resultMap = JsonUtils.OBJ_MAPPER.readValue(metricsResult, HashMap.class);
if (!resultMap.get("CPU and Memory").contains("heapUsed=")) {
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
index 339eec28d..9e79062e7 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/engine/SwaggerProducerOperation.java
@@ -20,20 +20,15 @@
import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import io.servicecomb.swagger.invocation.AsyncResponse;
import io.servicecomb.swagger.invocation.Response;
import io.servicecomb.swagger.invocation.SwaggerInvocation;
import io.servicecomb.swagger.invocation.arguments.producer.ProducerArgumentsMapper;
import io.servicecomb.swagger.invocation.context.ContextUtils;
-import io.servicecomb.swagger.invocation.exception.InvocationException;
+import io.servicecomb.swagger.invocation.exception.ExceptionFactory;
import io.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper;
public class SwaggerProducerOperation {
- private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerProducerOperation.class);
-
private String name;
// ????aop??????producerClass?????producerInstance.getClass()
@@ -132,10 +127,10 @@ public void doCompletableFutureInvoke(SwaggerInvocation invocation, AsyncRespons
return;
}
- asyncResp.handle(processException(ex));
+ asyncResp.handle(processException(invocation, ex));
});
} catch (Throwable e) {
- asyncResp.handle(processException(e));
+ asyncResp.handle(processException(invocation, e));
}
}
@@ -153,25 +148,16 @@ public Response doInvoke(SwaggerInvocation invocation) {
Object result = producerMethod.invoke(producerInstance, args);
response = responseMapper.mapResponse(invocation.getStatus(), result);
} catch (Throwable e) {
- response = processException(e);
+ response = processException(invocation, e);
}
return response;
}
- protected Response processException(Throwable e) {
+ protected Response processException(SwaggerInvocation invocation, Throwable e) {
if (InvocationTargetException.class.isInstance(e)) {
e = ((InvocationTargetException) e).getTargetException();
}
- if (InvocationException.class.isInstance(e)) {
- return Response.failResp((InvocationException) e);
- }
-
- // ???????????????
- Response response = Response.producerFailResp(e);
- String msg =
- String.format("Producer invoke failed, %s:%s", producerClass.getName(), producerMethod.getName());
- LOGGER.error(msg, e);
- return response;
+ return ExceptionFactory.convertExceptionToResponse(invocation, e);
}
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/SwaggerInvocation.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/SwaggerInvocation.java
index be102dd99..277b8968d 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/SwaggerInvocation.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/SwaggerInvocation.java
@@ -53,4 +53,8 @@ public void setSwaggerArguments(Object[] swaggerArguments) {
public void setSwaggerArgument(int idx, Object swaggerArgument) {
this.swaggerArguments[idx] = swaggerArgument;
}
+
+ public String getInvocationQualifiedName() {
+ return invocationType.name();
+ }
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java
new file mode 100644
index 000000000..b599ce854
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/DefaultExceptionToResponseConverter.java
@@ -0,0 +1,39 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+
+public class DefaultExceptionToResponseConverter implements ExceptionToResponseConverter<Throwable> {
+ private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionToResponseConverter.class);
+
+ @Override
+ public Class<Throwable> getExceptionClass() {
+ // default logic, not bind to special class
+ return null;
+ }
+
+ @Override
+ public Response convert(SwaggerInvocation swaggerInvocation, Throwable e) {
+ LOGGER.error("invoke failed, invocation={}", swaggerInvocation.getInvocationQualifiedName(), e);
+ return Response.producerFailResp(e);
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionFactory.java
index 821e0a60e..d4dc569c0 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionFactory.java
@@ -20,6 +20,8 @@
import javax.ws.rs.core.Response.StatusType;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
import io.servicecomb.swagger.invocation.context.HttpStatus;
public final class ExceptionFactory {
@@ -42,6 +44,8 @@
public static final String CONSUMER_INNER_REASON_PHRASE = "Cse Internal Bad Request";
+ private static ExceptionToResponseConverters exceptionToResponseConverters = new ExceptionToResponseConverters();
+
public static final StatusType CONSUMER_INNER_STATUS =
new HttpStatus(CONSUMER_INNER_STATUS_CODE, CONSUMER_INNER_REASON_PHRASE);
@@ -119,4 +123,8 @@ protected static InvocationException convertException(int statusCode, String rea
CommonExceptionData data = new CommonExceptionData(errorMsg);
return doCreate(statusCode, reasonPhrase, data, e);
}
+
+ public static Response convertExceptionToResponse(SwaggerInvocation swaggerInvocation, Throwable e) {
+ return exceptionToResponseConverters.convertExceptionToResponse(swaggerInvocation, e);
+ }
}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.java
new file mode 100644
index 000000000..3a3e9e044
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverter.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 io.servicecomb.swagger.invocation.exception;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+
+public interface ExceptionToResponseConverter<T extends Throwable> {
+ Class<T> getExceptionClass();
+
+ Response convert(SwaggerInvocation swaggerInvocation, T e);
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java
new file mode 100644
index 000000000..31a8a0c7c
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/ExceptionToResponseConverters.java
@@ -0,0 +1,49 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import io.servicecomb.foundation.common.utils.SPIServiceUtils;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+
+public class ExceptionToResponseConverters {
+ private Map<Class<?>, ExceptionToResponseConverter<Throwable>> exceptionToResponseConverters =
+ new HashMap<>();
+
+ private ExceptionToResponseConverter<Throwable> defaultConverter;
+
+ @SuppressWarnings("unchecked")
+ public ExceptionToResponseConverters() {
+ SPIServiceUtils.getAllService(ExceptionToResponseConverter.class).forEach(converter -> {
+ if (converter.getExceptionClass() == null) {
+ defaultConverter = converter;
+ return;
+ }
+
+ exceptionToResponseConverters.put(converter.getExceptionClass(), converter);
+ });
+ }
+
+ public Response convertExceptionToResponse(SwaggerInvocation swaggerInvocation, Throwable e) {
+ ExceptionToResponseConverter<Throwable> converter =
+ exceptionToResponseConverters.getOrDefault(e.getClass(), defaultConverter);
+ return converter.convert(swaggerInvocation, e);
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java
new file mode 100644
index 000000000..43b1ee832
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/io/servicecomb/swagger/invocation/exception/InvocationExceptionToResponseConverter.java
@@ -0,0 +1,32 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+
+public class InvocationExceptionToResponseConverter implements ExceptionToResponseConverter<InvocationException> {
+ @Override
+ public Class<InvocationException> getExceptionClass() {
+ return InvocationException.class;
+ }
+
+ @Override
+ public Response convert(SwaggerInvocation swaggerInvocation, InvocationException e) {
+ return Response.failResp(e);
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter
new file mode 100644
index 000000000..1314c539c
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/main/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+io.servicecomb.swagger.invocation.exception.InvocationExceptionToResponseConverter
+io.servicecomb.swagger.invocation.exception.DefaultExceptionToResponseConverter
\ No newline at end of file
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/engine/TestSwaggerProducerOperation.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/engine/TestSwaggerProducerOperation.java
new file mode 100644
index 000000000..d6bec6cf3
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/engine/TestSwaggerProducerOperation.java
@@ -0,0 +1,51 @@
+/*
+ * 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 io.servicecomb.swagger.engine;
+
+import java.lang.reflect.InvocationTargetException;
+
+import javax.ws.rs.core.Response.Status;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import mockit.Mocked;
+
+public class TestSwaggerProducerOperation {
+ SwaggerProducerOperation swaggerProducerOperation = new SwaggerProducerOperation();
+
+ @Test
+ public void processException_normal(@Mocked SwaggerInvocation invocation) {
+ Error error = new Error("abc");
+
+ Response response = swaggerProducerOperation.processException(invocation, error);
+ Assert.assertSame(Status.OK, response.getStatus());
+ Assert.assertEquals("response from error: abc", response.getResult());
+ }
+
+ @Test
+ public void processException_InvocationTargetException(@Mocked SwaggerInvocation invocation) {
+ Error error = new Error("abc");
+ InvocationTargetException targetException = new InvocationTargetException(error);
+
+ Response response = swaggerProducerOperation.processException(invocation, targetException);
+ Assert.assertSame(Status.OK, response.getStatus());
+ Assert.assertEquals("response from error: abc", response.getResult());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java
new file mode 100644
index 000000000..92bb4ffde
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/ErrorToResponseConverter.java
@@ -0,0 +1,34 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import javax.ws.rs.core.Response.Status;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+
+public class ErrorToResponseConverter implements ExceptionToResponseConverter<Error> {
+ @Override
+ public Class<Error> getExceptionClass() {
+ return Error.class;
+ }
+
+ @Override
+ public Response convert(SwaggerInvocation swaggerInvocation, Error e) {
+ return Response.create(Status.OK, "response from error: " + e.getMessage());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java
new file mode 100644
index 000000000..b759d2da9
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestDefaultExceptionToResponseConverter.java
@@ -0,0 +1,39 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import mockit.Mocked;
+
+public class TestDefaultExceptionToResponseConverter {
+ DefaultExceptionToResponseConverter converter = new DefaultExceptionToResponseConverter();
+
+ @Test
+ public void getExceptionClass() {
+ Assert.assertNull(converter.getExceptionClass());
+ }
+
+ @Test
+ public void convert(@Mocked SwaggerInvocation swaggerInvocation, @Mocked Error e) {
+ Response response = converter.convert(swaggerInvocation, e);
+ Assert.assertSame(e, ((InvocationException) response.getResult()).getCause());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionFactory.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionFactory.java
new file mode 100644
index 000000000..73883c3ab
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionFactory.java
@@ -0,0 +1,35 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import javax.ws.rs.core.Response.Status;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.swagger.invocation.Response;
+
+public class TestExceptionFactory {
+ @Test
+ public void convertExceptionToResponse() {
+ Error error = new Error("test");
+ Response response = ExceptionFactory.convertExceptionToResponse(null, error);
+
+ Assert.assertSame(Status.OK, response.getStatus());
+ Assert.assertEquals("response from error: test", response.getResult());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java
new file mode 100644
index 000000000..c4a7ce2dc
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestExceptionToResponseConverters.java
@@ -0,0 +1,71 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import java.util.Arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.foundation.common.utils.SPIServiceUtils;
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import mockit.Expectations;
+import mockit.Mocked;
+
+public class TestExceptionToResponseConverters {
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ public void convertExceptionToResponse(@Mocked ExceptionToResponseConverter c1,
+ @Mocked Response r1,
+ @Mocked ExceptionToResponseConverter c2,
+ @Mocked Response r2,
+ @Mocked ExceptionToResponseConverter cDef,
+ @Mocked Response rDef) {
+ new Expectations(SPIServiceUtils.class) {
+ {
+ SPIServiceUtils.getAllService(ExceptionToResponseConverter.class);
+ result = Arrays.asList(c1, c2, cDef);
+
+ c1.getExceptionClass();
+ result = Throwable.class;
+ c1.convert((SwaggerInvocation) any, (Throwable) any);
+ result = r1;
+
+ c2.getExceptionClass();
+ result = Exception.class;
+ c2.convert((SwaggerInvocation) any, (Throwable) any);
+ result = r2;
+
+ cDef.getExceptionClass();
+ result = null;
+ cDef.convert((SwaggerInvocation) any, (Throwable) any);
+ result = rDef;
+ }
+ };
+
+ ExceptionToResponseConverters exceptionToResponseConverters = new ExceptionToResponseConverters();
+
+ Assert.assertSame(r1,
+ exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, new Throwable()));
+ Assert.assertSame(r2,
+ exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null, new Exception()));
+ Assert.assertSame(rDef,
+ exceptionToResponseConverters.convertExceptionToResponse((SwaggerInvocation) null,
+ new IllegalStateException()));
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java
new file mode 100644
index 000000000..a103604e5
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/io/servicecomb/swagger/invocation/exception/TestInvocationExceptionToResponseConverter.java
@@ -0,0 +1,39 @@
+/*
+ * 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 io.servicecomb.swagger.invocation.exception;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.swagger.invocation.Response;
+import io.servicecomb.swagger.invocation.SwaggerInvocation;
+import mockit.Mocked;
+
+public class TestInvocationExceptionToResponseConverter {
+ InvocationExceptionToResponseConverter converter = new InvocationExceptionToResponseConverter();
+
+ @Test
+ public void getExceptionClass() {
+ Assert.assertEquals(InvocationException.class, converter.getExceptionClass());
+ }
+
+ @Test
+ public void convert(@Mocked SwaggerInvocation swaggerInvocation, @Mocked InvocationException e) {
+ Response response = converter.convert(swaggerInvocation, e);
+ Assert.assertSame(e, response.getResult());
+ }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter b/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter
new file mode 100644
index 000000000..bb5a352ce
--- /dev/null
+++ b/swagger/swagger-invocation/invocation-core/src/test/resources/META-INF/services/io.servicecomb.swagger.invocation.exception.ExceptionToResponseConverter
@@ -0,0 +1 @@
+io.servicecomb.swagger.invocation.exception.ErrorToResponseConverter
\ No newline at end of file
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services