You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/04/05 07:20:16 UTC

[shardingsphere] branch master updated: Add error code for ShardingSphereSPIException (#16597)

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

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 326922595cf Add error code for ShardingSphereSPIException (#16597)
326922595cf is described below

commit 326922595cfccf61a09def974b62fab7ad3bbf4a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Apr 5 15:20:07 2022 +0800

    Add error code for ShardingSphereSPIException (#16597)
---
 .../ServiceLoaderInstantiationException.java       |  4 +++-
 .../ServiceProviderNotFoundException.java          |  6 ++++--
 .../spi/exception/ShardingSphereSPIException.java  | 12 ++++++++----
 .../ServiceLoaderInstantiationExceptionTest.java}  | 18 ++++++++++--------
 .../ServiceProviderNotFoundExceptionTest.java}     | 22 ++++++++++++----------
 5 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
index 48ccb12daa3..3c3365d5134 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
@@ -24,7 +24,9 @@ public final class ServiceLoaderInstantiationException extends ShardingSphereSPI
     
     private static final long serialVersionUID = 6261274443437676201L;
     
+    private static final int ERROR_CODE = 2;
+    
     public ServiceLoaderInstantiationException(final Class<?> clazz, final Exception cause) {
-        super(String.format("Can not find public default constructor for SPI class `%s`", clazz.getName()), cause);
+        super(ERROR_CODE, String.format("Can not find public default constructor for SPI class `%s`.", clazz.getName()), cause);
     }
 }
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java
index d859383c894..1779ecc480f 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java
@@ -24,11 +24,13 @@ public final class ServiceProviderNotFoundException extends ShardingSphereSPIExc
     
     private static final long serialVersionUID = -3730257541332863236L;
     
+    private static final int ERROR_CODE = 1;
+    
     public ServiceProviderNotFoundException(final Class<?> clazz) {
-        super(String.format("No implementation class load from SPI `%s`.", clazz.getName()));
+        super(ERROR_CODE, String.format("No implementation class load from SPI `%s`.", clazz.getName()));
     }
     
     public ServiceProviderNotFoundException(final Class<?> clazz, final String type) {
-        super(String.format("No implementation class load from SPI `%s` with type `%s`.", clazz.getName(), type));
+        super(ERROR_CODE, String.format("No implementation class load from SPI `%s` with type `%s`.", clazz.getName(), type));
     }
 }
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
index 7e194f95d4e..bf82427fd2f 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ShardingSphereSPIException.java
@@ -24,11 +24,15 @@ public abstract class ShardingSphereSPIException extends RuntimeException {
     
     private static final long serialVersionUID = -3044979409127407014L;
     
-    public ShardingSphereSPIException(final String message) {
-        super(message);
+    public ShardingSphereSPIException(final int errorCode, final String message) {
+        super(createErrorMessage(errorCode, message));
     }
     
-    public ShardingSphereSPIException(final String message, final Exception cause) {
-        super(message, cause);
+    public ShardingSphereSPIException(final int errorCode, final String message, final Exception cause) {
+        super(createErrorMessage(errorCode, message), cause);
+    }
+    
+    private static String createErrorMessage(final int errorCode, final String message) {
+        return String.format("SPI-%05d: %s", errorCode, message);
     }
 }
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java
similarity index 66%
copy from shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
copy to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java
index 48ccb12daa3..fa7b1bf1618 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationException.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceLoaderInstantiationExceptionTest.java
@@ -17,14 +17,16 @@
 
 package org.apache.shardingsphere.spi.exception;
 
-/**
- * Service loader instantiation exception.
- */
-public final class ServiceLoaderInstantiationException extends ShardingSphereSPIException {
-    
-    private static final long serialVersionUID = 6261274443437676201L;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class ServiceLoaderInstantiationExceptionTest {
     
-    public ServiceLoaderInstantiationException(final Class<?> clazz, final Exception cause) {
-        super(String.format("Can not find public default constructor for SPI class `%s`", clazz.getName()), cause);
+    @Test
+    public void assertGetMessage() {
+        assertThat(new ServiceLoaderInstantiationException(Object.class, new RuntimeException()).getMessage(),
+                is("SPI-00002: Can not find public default constructor for SPI class `java.lang.Object`."));
     }
 }
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundExceptionTest.java
similarity index 57%
copy from shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java
copy to shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundExceptionTest.java
index d859383c894..1adb4f204a9 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundException.java
+++ b/shardingsphere-spi/src/test/java/org/apache/shardingsphere/spi/exception/ServiceProviderNotFoundExceptionTest.java
@@ -17,18 +17,20 @@
 
 package org.apache.shardingsphere.spi.exception;
 
-/**
- * Service provider not found exception.
- */
-public final class ServiceProviderNotFoundException extends ShardingSphereSPIException {
-    
-    private static final long serialVersionUID = -3730257541332863236L;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class ServiceProviderNotFoundExceptionTest {
     
-    public ServiceProviderNotFoundException(final Class<?> clazz) {
-        super(String.format("No implementation class load from SPI `%s`.", clazz.getName()));
+    @Test
+    public void assertGetMessageWithClassOnly() {
+        assertThat(new ServiceProviderNotFoundException(Object.class).getMessage(), is("SPI-00001: No implementation class load from SPI `java.lang.Object`."));
     }
     
-    public ServiceProviderNotFoundException(final Class<?> clazz, final String type) {
-        super(String.format("No implementation class load from SPI `%s` with type `%s`.", clazz.getName(), type));
+    @Test
+    public void assertGetMessageWithClassAndType() {
+        assertThat(new ServiceProviderNotFoundException(Object.class, "Foo").getMessage(), is("SPI-00001: No implementation class load from SPI `java.lang.Object` with type `Foo`."));
     }
 }