From 12d31db6a8aca57a77123d03ddc1b40d9d027064 Mon Sep 17 00:00:00 2001 From: kl Date: Wed, 15 Mar 2023 11:16:14 +0800 Subject: [PATCH 1/3] refactor(apollo-client): Optimize the exception message when failing to retrieve configuration information. --- .../com/ctrip/framework/apollo/util/ExceptionUtil.java | 10 +++++++++- .../ctrip/framework/apollo/util/ExceptionUtilTest.java | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java index 0a179d97..ed97ca6c 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java @@ -52,11 +52,19 @@ public static String getDetailMessage(Throwable ex) { counter--; continue; } - builder.append(" [Cause: ").append(cause.getMessage()); + builder.append(" [Cause: ") + .append(getThrowingClassName(cause)) + .append("(") + .append(cause.getMessage()) + .append(")"); } builder.append(Strings.repeat("]", counter)); return builder.toString(); } + + public static String getThrowingClassName(Throwable throwable) { + return throwable.getClass().getSimpleName(); + } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java index 1077c41a..e8d3d671 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ExceptionUtilTest.java @@ -38,11 +38,11 @@ public void testGetDetailMessageWithCauses() throws Exception { String causeMsg2 = "another cause"; String someMessage = "some message"; - Throwable cause2 = new Throwable(causeMsg2); + Throwable cause2 = new Exception(causeMsg2); Throwable cause1 = new Throwable(causeMsg1, cause2); Throwable ex = new Throwable(someMessage, cause1); - String expected = someMessage + " [Cause: " + causeMsg1 + " [Cause: " + causeMsg2 + "]]"; + String expected = someMessage + " [Cause: Throwable("+ causeMsg1 +") [Cause: Exception(" + causeMsg2 + ")]]"; assertEquals(expected, ExceptionUtil.getDetailMessage(ex)); } From ef4a6e77156d369d64b6af45fbd7d5937ff8602e Mon Sep 17 00:00:00 2001 From: kl Date: Wed, 15 Mar 2023 11:31:28 +0800 Subject: [PATCH 2/3] chore(changes): Update CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ec72fc2e..5df63b99 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,6 @@ Release Notes. Apollo Java 2.2.0 ------------------ - +[refactor(apollo-client): Optimize the exception message when failing to retrieve configuration information.](https://github.com/apolloconfig/apollo-java/pull/22) ------------------ All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/2?closed=1) \ No newline at end of file From bfe1c07958184b3043298cc045c0ae011cede77b Mon Sep 17 00:00:00 2001 From: kl Date: Thu, 16 Mar 2023 10:06:24 +0800 Subject: [PATCH 3/3] chore(apollo-java): Streamlined code --- .../java/com/ctrip/framework/apollo/util/ExceptionUtil.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java index ed97ca6c..28df3f5f 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ExceptionUtil.java @@ -53,7 +53,7 @@ public static String getDetailMessage(Throwable ex) { continue; } builder.append(" [Cause: ") - .append(getThrowingClassName(cause)) + .append(cause.getClass().getSimpleName()) .append("(") .append(cause.getMessage()) .append(")"); @@ -64,7 +64,4 @@ public static String getDetailMessage(Throwable ex) { return builder.toString(); } - public static String getThrowingClassName(Throwable throwable) { - return throwable.getClass().getSimpleName(); - } }