Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve] Add PulsarExceptionBase class that supports slf4j like parameterized string formatting #18968

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}