From a23bcbab61e47a72a37f8dfba18fb5850ba3071b Mon Sep 17 00:00:00 2001 From: Ali Ahmed Date: Sat, 17 Dec 2022 06:20:27 -0800 Subject: [PATCH] Add PulsarExceptionBase class that supports slf4j like parameterized string formatting --- .../common/exception/PulsarExceptionBase.java | 56 ++++++++++++++++++ .../pulsar/common/exception/package-info.java | 22 +++++++ .../exception/PulsarExceptionSample.java | 44 ++++++++++++++ .../pulsar/exception/PulsarExceptionTest.java | 58 +++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 pulsar-common/src/main/java/org/apache/pulsar/common/exception/PulsarExceptionBase.java create mode 100644 pulsar-common/src/main/java/org/apache/pulsar/common/exception/package-info.java create mode 100644 pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionSample.java create mode 100644 pulsar-common/src/test/java/org/apache/pulsar/exception/PulsarExceptionTest.java 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 0000000000000..41b5d13892055 --- /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 0000000000000..f5315be7022c1 --- /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 0000000000000..09a5063a0a4a8 --- /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 0000000000000..2508c8f4beeaf --- /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"); + } +}