You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/02 10:44:50 UTC
[incubator-servicecomb-java-chassis] 02/03: JAV-575
SwaggerProducerOperation allow inject ExceptionToResponseConverter
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit e01ee9ca303717453a4b26838f0372dacf407a5f
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Dec 27 15:33:03 2017 +0800
JAV-575 SwaggerProducerOperation allow inject ExceptionToResponseConverter
---
.../swagger/engine/SwaggerProducerOperation.java | 26 +++--------
.../engine/TestSwaggerProducerOperation.java | 51 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 20 deletions(-)
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 339eec2..9e79062 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.InvocationTargetException;
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 class SwaggerProducerOperation {
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 class SwaggerProducerOperation {
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/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 0000000..d6bec6c
--- /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());
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.