You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/06/08 01:18:20 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1992] tiny improve about InvocationException

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new f72b125  [SCB-1992] tiny improve about InvocationException
f72b125 is described below

commit f72b125376b95dcb21802a7f62e6f50e501fc051
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Jun 5 21:40:34 2020 +0800

    [SCB-1992] tiny improve about InvocationException
---
 .../common/rest/RestProducerInvocationCreatorTest.java   |  4 ++--
 .../rest/filter/inner/RestServerCodecFilterTest.java     |  2 +-
 .../servicecomb/core/exception/ExceptionCodes.java       | 10 +++++-----
 .../apache/servicecomb/core/exception/Exceptions.java    | 11 +++++++----
 .../converter/ConstraintViolationExceptionConverter.java |  4 +++-
 .../converter/IllegalArgumentExceptionConverter.java     |  4 +++-
 .../converter/InvocationExceptionConverter.java          |  4 +++-
 core/src/main/resources/microservice.yaml                |  4 ++--
 .../servicecomb/core/exception/ExceptionsTest.java       |  4 ++--
 .../core/invocation/ProducerInvocationFlowTest.java      |  4 ++--
 .../servicecomb/foundation/common/DynamicObject.java     |  3 ++-
 .../springmvc/tests/SpringMvcIntegrationTestBase.java    |  2 +-
 .../tracing-tests/src/test/resources/microservice.yaml   |  2 +-
 .../invocation/exception/CommonExceptionData.java        | 16 +++++++++++++---
 .../swagger/invocation/exception/ExceptionFactory.java   |  5 +++--
 .../invocation/exception/InvocationException.java        | 13 +++++++------
 .../transport/highway/HighwayServerCodecFilterTest.java  |  2 +-
 .../rest/client/RestTransportClientManager.java          |  2 +-
 18 files changed, 59 insertions(+), 37 deletions(-)

diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java
index a79b1fc..b1dc7ff 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java
@@ -119,7 +119,7 @@ public class RestProducerInvocationCreatorTest {
     CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
 
     assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
-    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.0002\",\"message\":\"Not Found\"}");
+    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
   }
 
   @Test
@@ -140,7 +140,7 @@ public class RestProducerInvocationCreatorTest {
 
     assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
     assertThat(Json.encode(data))
-        .isEqualTo("{\"code\":\"SCB.0000\",\"message\":\"Accept test-type is not supported\"}");
+        .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
   }
 
   @Test
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilterTest.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilterTest.java
index a937d51..2e6fe77 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilterTest.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/filter/inner/RestServerCodecFilterTest.java
@@ -147,7 +147,7 @@ public class RestServerCodecFilterTest {
 
     assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR);
     assertThat(Json.encode(response.getResult()))
-        .isEqualTo("{\"code\":\"SCB.5000\",\"message\":\"encode request failed\"}");
+        .isEqualTo("{\"code\":\"SCB.50000000\",\"message\":\"encode request failed\"}");
   }
 
   private void success_invocation() throws InterruptedException, ExecutionException {
diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/ExceptionCodes.java b/core/src/main/java/org/apache/servicecomb/core/exception/ExceptionCodes.java
index c47e5b8..f28b4e6 100644
--- a/core/src/main/java/org/apache/servicecomb/core/exception/ExceptionCodes.java
+++ b/core/src/main/java/org/apache/servicecomb/core/exception/ExceptionCodes.java
@@ -17,10 +17,10 @@
 package org.apache.servicecomb.core.exception;
 
 public interface ExceptionCodes {
-  String GENERIC_CLIENT = "SCB.0000";
-  String LB_ADDRESS_NOT_FOUND = "SCB.0001";
-  String NOT_DEFINED_ANY_SCHEMA = "SCB.0002";
-  String DEFAULT_VALIDATE = "SCB.0003";
+  String GENERIC_CLIENT = "SCB.00000000";
+  String LB_ADDRESS_NOT_FOUND = "SCB.00000001";
+  String NOT_DEFINED_ANY_SCHEMA = "SCB.00000002";
+  String DEFAULT_VALIDATE = "SCB.00000003";
 
-  String GENERIC_SERVER = "SCB.5000";
+  String GENERIC_SERVER = "SCB.50000000";
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/Exceptions.java b/core/src/main/java/org/apache/servicecomb/core/exception/Exceptions.java
index cbffc13..fa6115f 100644
--- a/core/src/main/java/org/apache/servicecomb/core/exception/Exceptions.java
+++ b/core/src/main/java/org/apache/servicecomb/core/exception/Exceptions.java
@@ -28,7 +28,6 @@ import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.Response.StatusType;
 
 import org.apache.servicecomb.core.Invocation;
@@ -54,15 +53,19 @@ public final class Exceptions {
     return ExceptionFactory.unwrapIncludeInvocationException(throwable);
   }
 
-  public static Throwable unwrap(Throwable throwable) {
+  public static <T extends Throwable> T unwrap(Throwable throwable) {
     return ExceptionFactory.unwrap(throwable);
   }
 
-  public static InvocationException create(Status status, String code, String msg) {
+  public static InvocationException create(StatusType status, Object errorData) {
+    return new InvocationException(status, errorData);
+  }
+
+  public static InvocationException create(StatusType status, String code, String msg) {
     return new InvocationException(status, code, msg);
   }
 
-  private static InvocationException create(Status status, String code, String msg, Throwable cause) {
+  private static InvocationException create(StatusType status, String code, String msg, Throwable cause) {
     return new InvocationException(status, code, msg, cause);
   }
 
diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java b/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
index 2e6c262..5006b22 100644
--- a/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
+++ b/core/src/main/java/org/apache/servicecomb/core/exception/converter/ConstraintViolationExceptionConverter.java
@@ -35,6 +35,8 @@ import com.netflix.config.DynamicPropertyFactory;
 import com.netflix.config.DynamicStringProperty;
 
 public class ConstraintViolationExceptionConverter implements ExceptionConverter<ConstraintViolationException> {
+  public static final short ORDER = Short.MAX_VALUE;
+
   public static final String KEY_CODE = "servicecomb.filters.validate.code";
 
   private DynamicStringProperty code;
@@ -53,7 +55,7 @@ public class ConstraintViolationExceptionConverter implements ExceptionConverter
 
   @Override
   public int getOrder() {
-    return Short.MAX_VALUE;
+    return ORDER;
   }
 
   @Override
diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java b/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
index e4187da..9d17d46 100644
--- a/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
+++ b/core/src/main/java/org/apache/servicecomb/core/exception/converter/IllegalArgumentExceptionConverter.java
@@ -24,9 +24,11 @@ import org.apache.servicecomb.core.exception.ExceptionConverter;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 
 public class IllegalArgumentExceptionConverter implements ExceptionConverter<IllegalArgumentException> {
+  public static final short ORDER = Short.MAX_VALUE;
+
   @Override
   public int getOrder() {
-    return Short.MAX_VALUE;
+    return ORDER;
   }
 
   @Override
diff --git a/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java b/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
index daa0983..55ab688 100644
--- a/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
+++ b/core/src/main/java/org/apache/servicecomb/core/exception/converter/InvocationExceptionConverter.java
@@ -24,9 +24,11 @@ import org.apache.servicecomb.core.exception.ExceptionConverter;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 
 public class InvocationExceptionConverter implements ExceptionConverter<InvocationException> {
+  public static final byte ORDER = Byte.MAX_VALUE;
+
   @Override
   public int getOrder() {
-    return Byte.MAX_VALUE;
+    return ORDER;
   }
 
   @Override
diff --git a/core/src/main/resources/microservice.yaml b/core/src/main/resources/microservice.yaml
index f3b9fa3..7d10f60 100644
--- a/core/src/main/resources/microservice.yaml
+++ b/core/src/main/resources/microservice.yaml
@@ -24,7 +24,7 @@ servicecomb:
       #default-consumer-transport:
       #  rest: rest-client-codec
       #  highway: highway-client-codec
-      default-producer-tranport:
+      default-producer-transport:
         rest: rest-server-codec
         highway: highway-server-codec
     consumer:
@@ -34,7 +34,7 @@ servicecomb:
       #policies:
       #  ms-1: retry, load-balance, transport-client, ms-1-consumer-transport
     producer:
-      default: default-producer-tranport, schedule, producer-operation
+      default: default-producer-transport, schedule, producer-operation
       # samples for customize microservice filter chain
       #policies:
       #  ms-1: qps-limiter, ms-1-producer-transport, schedule, producer-operation
\ No newline at end of file
diff --git a/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java b/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
index 0025afb..fa6783f 100644
--- a/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
+++ b/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
@@ -46,7 +46,7 @@ class ExceptionsTest {
     assertThat(invocationException.getStatus()).isEqualTo(BAD_REQUEST);
     assertThat(invocationException.getErrorData()).isInstanceOf(CommonExceptionData.class);
     assertThat(Json.encode(invocationException.getErrorData()))
-        .isEqualTo("{\"code\":\"SCB.0000\",\"message\":\"msg\"}");
+        .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"msg\"}");
   }
 
   @Test
@@ -59,6 +59,6 @@ class ExceptionsTest {
     assertThat(invocationException.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR);
     assertThat(invocationException.getErrorData()).isInstanceOf(CommonExceptionData.class);
     assertThat(Json.encode(invocationException.getErrorData()))
-        .isEqualTo("{\"code\":\"SCB.5000\",\"message\":\"msg\"}");
+        .isEqualTo("{\"code\":\"SCB.50000000\",\"message\":\"msg\"}");
   }
 }
\ No newline at end of file
diff --git a/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java b/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java
index b31f6fa..b8c0d46 100644
--- a/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java
+++ b/core/src/test/java/org/apache/servicecomb/core/invocation/ProducerInvocationFlowTest.java
@@ -41,7 +41,7 @@ public class ProducerInvocationFlowTest {
 
     @Override
     protected Invocation sendCreateInvocationException(Throwable throwable) {
-      sendException = throwable;
+      sendException = Exceptions.unwrap(throwable);
       return null;
     }
 
@@ -72,7 +72,7 @@ public class ProducerInvocationFlowTest {
 
     flow.run();
 
-    assertThat(Exceptions.unwrap(sendException)).isSameAs(exception);
+    assertThat(sendException).isSameAs(exception);
   }
 
   private void mockFilterChain() {
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/DynamicObject.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/DynamicObject.java
index dfb38ed..0bdf703 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/DynamicObject.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/DynamicObject.java
@@ -40,7 +40,8 @@ public class DynamicObject {
   }
 
   @JsonAnySetter
-  public void putDynamic(String key, Object value) {
+  public DynamicObject putDynamic(String key, Object value) {
     this.dynamic.put(key, value);
+    return this;
   }
 }
diff --git a/integration-tests/springmvc-tests/common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcIntegrationTestBase.java b/integration-tests/springmvc-tests/common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcIntegrationTestBase.java
index fb186a3..64e0278 100644
--- a/integration-tests/springmvc-tests/common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcIntegrationTestBase.java
+++ b/integration-tests/springmvc-tests/common/src/test/java/org/apache/servicecomb/demo/springmvc/tests/SpringMvcIntegrationTestBase.java
@@ -639,7 +639,7 @@ public class SpringMvcIntegrationTestBase {
       assertThat(e.getResponseBodyAsString(),
           Matchers.isOneOf(
               "{\"message\":\"Unexpected exception when processing the request.\"}",
-              "{\"code\":\"SCB.5000\",\"message\":\"Unexpected exception when processing.\"}"));
+              "{\"code\":\"SCB.50000000\",\"message\":\"Unexpected exception when processing.\"}"));
     }
   }
 
diff --git a/integration-tests/tracing-tests/src/test/resources/microservice.yaml b/integration-tests/tracing-tests/src/test/resources/microservice.yaml
index bd4a59c..939decc 100644
--- a/integration-tests/tracing-tests/src/test/resources/microservice.yaml
+++ b/integration-tests/tracing-tests/src/test/resources/microservice.yaml
@@ -35,7 +35,7 @@ servicecomb:
         default: tracing-consumer,loadbalance,bizkeeper-consumer
   filter-chains:
     producer:
-      default: default-producer-tranport, schedule, zipkin, validator, producer-operation
+      default: default-producer-transport, schedule, zipkin, validator, producer-operation
   tracing:
     collector:
       address: http://localhost:9411
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/CommonExceptionData.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/CommonExceptionData.java
index c868448..fdec294 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/CommonExceptionData.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/CommonExceptionData.java
@@ -47,20 +47,30 @@ public class CommonExceptionData extends DynamicObject {
     return code;
   }
 
-  public void setCode(String code) {
+  public CommonExceptionData setCode(String code) {
     this.code = code;
+    return this;
   }
 
   public String getMessage() {
     return message;
   }
 
-  public void setMessage(String message) {
+  public CommonExceptionData setMessage(String message) {
     this.message = message;
+    return this;
   }
 
   @Override
   public String toString() {
-    return "CommonExceptionData [message=" + message + "]";
+    if (code == null) {
+      return "CommonExceptionData [message=" + message + "]";
+    }
+
+    return "CommonExceptionData{" +
+        "code='" + code + '\'' +
+        ", message='" + message + '\'' +
+        ", dynamic=" + dynamic +
+        '}';
   }
 }
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java
index 2265822..3fcc2a9 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/ExceptionFactory.java
@@ -136,7 +136,8 @@ public final class ExceptionFactory {
     return throwable;
   }
 
-  public static Throwable unwrap(Throwable throwable) {
+  @SuppressWarnings("unchecked")
+  public static <T extends Throwable> T unwrap(Throwable throwable) {
     if (throwable instanceof InvocationTargetException) {
       throwable = ((InvocationTargetException) throwable).getTargetException();
     }
@@ -146,6 +147,6 @@ public final class ExceptionFactory {
     if (throwable instanceof ExecutionException) {
       throwable = throwable.getCause();
     }
-    return throwable;
+    return (T) throwable;
   }
 }
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationException.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationException.java
index dc9c0b6..85dd01b 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationException.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/exception/InvocationException.java
@@ -84,6 +84,11 @@ public class InvocationException extends RuntimeException {
     return errorData;
   }
 
+  @SuppressWarnings("unchecked")
+  public <T> T getError() {
+    return (T) errorData;
+  }
+
   @Override
   public String getMessage() {
     return this.toString();
@@ -91,11 +96,7 @@ public class InvocationException extends RuntimeException {
 
   @Override
   public String toString() {
-    StringBuilder sb = new StringBuilder();
-    sb.append("InvocationException: code=");
-    sb.append(getStatusCode());
-    sb.append(";msg=");
-    sb.append(getErrorData());
-    return sb.toString();
+    return "InvocationException: code=" + getStatusCode()
+        + ";msg=" + getErrorData();
   }
 }
diff --git a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilterTest.java b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilterTest.java
index 70cfaaa..d259d60 100644
--- a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilterTest.java
+++ b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/HighwayServerCodecFilterTest.java
@@ -132,7 +132,7 @@ public class HighwayServerCodecFilterTest {
 
     assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR);
     assertThat(Json.encode(response.getResult()))
-        .isEqualTo("{\"code\":\"SCB.5000\",\"message\":\"encode request failed\"}");
+        .isEqualTo("{\"code\":\"SCB.50000000\",\"message\":\"encode request failed\"}");
   }
 
   private void success_invocation() throws InterruptedException, ExecutionException {
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClientManager.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClientManager.java
index 3dfd785..0555d40 100644
--- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClientManager.java
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/RestTransportClientManager.java
@@ -24,7 +24,7 @@ import io.vertx.core.Vertx;
 public final class RestTransportClientManager {
   public static final RestTransportClientManager INSTANCE = new RestTransportClientManager();
 
-  // same instance in AbstractTranport. need refactor in future.
+  // same instance in AbstractTransport. need refactor in future.
   private final Vertx transportVertx = VertxUtils.getOrCreateVertxByName("transport", null);
 
   private RestTransportClient restClient;