You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by aa...@apache.org on 2022/12/22 03:22:55 UTC
[pulsar] branch master updated: [improve] Add PulsarExceptionBase class that supports slf4j like parameterized string formatting (#18968)
This is an automated email from the ASF dual-hosted git repository.
aahmed pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 89908552586 [improve] Add PulsarExceptionBase class that supports slf4j like parameterized string formatting (#18968)
89908552586 is described below
commit 899085525861ffb5e52d99a0fdb620ed97ae494a
Author: Ali Ahmed <al...@gmail.com>
AuthorDate: Wed Dec 21 19:22:49 2022 -0800
[improve] Add PulsarExceptionBase class that supports slf4j like parameterized string formatting (#18968)
Co-authored-by: Ali Ahmed <al...@splunk.com>
---
.../common/exception/PulsarExceptionBase.java | 56 +++++++++++++++++++++
.../pulsar/common/exception/package-info.java | 22 ++++++++
.../pulsar/exception/PulsarExceptionSample.java | 44 ++++++++++++++++
.../pulsar/exception/PulsarExceptionTest.java | 58 ++++++++++++++++++++++
4 files changed, 180 insertions(+)
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/exception/PulsarExceptionBase.java b/pulsar-common/src/main/java/org/apache/pulsar/common/exception/PulsarExceptionBase.java
new file mode 100644
index 00000000000..41b5d138920
--- /dev/null
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/exception/PulsarExceptionBase.java
@@ -0,0 +1,56 @@
+/*
+ * 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 org.apache.pulsar.common.exception;
+
+import org.slf4j.helpers.MessageFormatter;
+
+public abstract class PulsarExceptionBase extends Exception {
+
+ protected static String format(String message, Object... os) {
+ Throwable throwableCandidate = MessageFormatter.getThrowableCandidate(os);
+ Object[] args = os;
+ if (throwableCandidate != null) {
+ args = MessageFormatter.trimmedCopy(os);
+ }
+ return MessageFormatter.arrayFormat(message, args, null).getMessage();
+ }
+
+ private static final long serialVersionUID = -2343132778619884518L;
+
+ protected PulsarExceptionBase() {
+ super();
+ }
+
+ protected PulsarExceptionBase(String message) {
+ super(message);
+ }
+
+ protected PulsarExceptionBase(String message, Object... os) {
+ this(format(message, os), MessageFormatter.getThrowableCandidate(os));
+ }
+
+ protected PulsarExceptionBase(Throwable cause) {
+ super(cause);
+ }
+
+ protected PulsarExceptionBase(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
\ No newline at end of file
diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/exception/package-info.java b/pulsar-common/src/main/java/org/apache/pulsar/common/exception/package-info.java
new file mode 100644
index 00000000000..f5315be7022
--- /dev/null
+++ b/pulsar-common/src/main/java/org/apache/pulsar/common/exception/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+/**
+ * Exception classes
+ */
+package org.apache.pulsar.common.exception;
\ No newline at end of file
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionSample.java b/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionSample.java
new file mode 100644
index 00000000000..09a5063a0a4
--- /dev/null
+++ b/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionSample.java
@@ -0,0 +1,44 @@
+/*
+ * 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 org.apache.pulsar.exception;
+
+import org.apache.pulsar.common.exception.PulsarExceptionBase;
+
+public class PulsarExceptionSample extends PulsarExceptionBase {
+
+ protected PulsarExceptionSample() {
+ super();
+ }
+
+ protected PulsarExceptionSample(String message) {
+ super(message);
+ }
+
+ protected PulsarExceptionSample(String message, Object... os) {
+ super(message, os);
+ }
+
+ protected PulsarExceptionSample(Throwable cause) {
+ super(cause);
+ }
+
+ protected PulsarExceptionSample(String message, Throwable cause) {
+ super(message, cause);
+ }
+}
diff --git a/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionTest.java b/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionTest.java
new file mode 100644
index 00000000000..2508c8f4bee
--- /dev/null
+++ b/pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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 org.apache.pulsar.exception;
+
+import org.testng.annotations.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PulsarExceptionTest {
+
+ @Test
+ public void testPulsarExceptionWithParameters() {
+
+ String key = "123";
+ String value = "abc";
+
+ PulsarExceptionSample testException =
+ new PulsarExceptionSample("key={}, value={}", key, value);
+ assertThat(testException.getMessage()).isEqualTo("key=123, value=abc");
+ }
+
+ @Test
+ public void testPulsarExceptionSimple() {
+
+ PulsarExceptionSample testException =
+ new PulsarExceptionSample("Simple Message");
+ assertThat(testException.getMessage()).isEqualTo("Simple Message");
+ }
+
+ @Test
+ public void testPulsarExceptionCompound() {
+
+ int key = 123;
+ String value = "abc";
+
+ PulsarExceptionSample testException =
+ new PulsarExceptionSample("key={}, value={}", key, value, new Exception("inner exception"));
+
+ assertThat(testException.getMessage()).contains("key=123, value=abc");
+ assertThat(testException.getCause().getMessage()).isEqualTo("inner exception");
+ }
+}