KAFKA-16409: DeleteRecordsCommand should use standard exception handling (#15586)

DeleteRecordsCommand should use standard exception handling

Reviewers: Luke Chen <showuon@gmail.com>
This commit is contained in:
PoAn Yang 2024-03-26 08:44:59 +08:00 committed by GitHub
parent ad960635a9
commit fa1cf7975e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.DeleteRecordsResult; import org.apache.kafka.clients.admin.DeleteRecordsResult;
import org.apache.kafka.clients.admin.RecordsToDelete; import org.apache.kafka.clients.admin.RecordsToDelete;
import org.apache.kafka.common.TopicPartition; import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.utils.Utils; import org.apache.kafka.common.utils.Utils;
import org.apache.kafka.server.common.AdminCommandFailedException; import org.apache.kafka.server.common.AdminCommandFailedException;
import org.apache.kafka.server.common.AdminOperationException; import org.apache.kafka.server.common.AdminOperationException;
@ -61,7 +62,18 @@ public class DeleteRecordsCommand {
private static final DecodeJson.DecodeString STRING = new DecodeJson.DecodeString(); private static final DecodeJson.DecodeString STRING = new DecodeJson.DecodeString();
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
execute(args, System.out); Exit.exit(mainNoExit(args));
}
static int mainNoExit(String... args) {
try {
execute(args, System.out);
return 0;
} catch (Exception e) {
System.err.println(e.getMessage());
System.err.println(Utils.stackTrace(e));
return 1;
}
} }
static Map<TopicPartition, List<Long>> parseOffsetJsonStringWithoutDedup(String jsonData) throws JsonProcessingException { static Map<TopicPartition, List<Long>> parseOffsetJsonStringWithoutDedup(String jsonData) throws JsonProcessingException {

View File

@ -120,7 +120,11 @@ public class DeleteRecordsCommandTest {
class DeleteRecordsCommandUnitTest { class DeleteRecordsCommandUnitTest {
@Test @Test
public void testOffsetFileNotExists() { public void testOffsetFileNotExists() {
assertThrows(IOException.class, () -> DeleteRecordsCommand.main(new String[]{ assertThrows(IOException.class, () -> DeleteRecordsCommand.execute(new String[]{
"--bootstrap-server", "localhost:9092",
"--offset-json-file", "/not/existing/file"
}, System.out));
assertEquals(1, DeleteRecordsCommand.mainNoExit(new String[]{
"--bootstrap-server", "localhost:9092", "--bootstrap-server", "localhost:9092",
"--offset-json-file", "/not/existing/file" "--offset-json-file", "/not/existing/file"
})); }));
@ -128,7 +132,12 @@ class DeleteRecordsCommandUnitTest {
@Test @Test
public void testCommandConfigNotExists() { public void testCommandConfigNotExists() {
assertThrows(NoSuchFileException.class, () -> DeleteRecordsCommand.main(new String[] { assertThrows(NoSuchFileException.class, () -> DeleteRecordsCommand.execute(new String[] {
"--bootstrap-server", "localhost:9092",
"--offset-json-file", "/not/existing/file",
"--command-config", "/another/not/existing/file"
}, System.out));
assertEquals(1, DeleteRecordsCommand.mainNoExit(new String[] {
"--bootstrap-server", "localhost:9092", "--bootstrap-server", "localhost:9092",
"--offset-json-file", "/not/existing/file", "--offset-json-file", "/not/existing/file",
"--command-config", "/another/not/existing/file" "--command-config", "/another/not/existing/file"