Skip to content

Commit

Permalink
💥 kcctl#17 Allow non 401 KafkaConnectExceptions to bubble up
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfosterdev committed Oct 7, 2021
1 parent a9bb7b1 commit 38e99a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ public String getErrorMessage() {
}

@Override
public int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult parseResult) {
public int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult parseResult) throws Exception {
var exitCodeErrorMessagePair = loadMessageAndExitCode(ex);
if (exitCodeErrorMessagePair == null) {
throw ex;
}

System.err.println(Colors.ANSI_RED + exitCodeErrorMessagePair.getErrorMessage() + Colors.ANSI_RESET);
return exitCodeErrorMessagePair.getExitCode();
}
Expand All @@ -60,7 +64,6 @@ public static ExitCodeErrorMessagePair loadMessageAndExitCode(Exception ex) {
}
}
}

return new ExitCodeErrorMessagePair(CommandLine.ExitCode.SOFTWARE, "An error occured.");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import picocli.CommandLine;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@DisplayNameGeneration(ReplaceUnderscores.class)
class ExecutionExceptionHandlerTest {
Expand Down Expand Up @@ -59,7 +60,7 @@ public String errorPrintLnFormatted(String str) {
}

@Test
void should_handle_unauthorized_errors() throws IOException {
void should_handle_unauthorized_errors() throws Exception {
var handler = new ExecutionExceptionHandler();
int exitCode = handler.handleExecutionException(
new KafkaConnectException("Unauthorized", 401), null, null);
Expand All @@ -71,25 +72,17 @@ void should_handle_unauthorized_errors() throws IOException {
}

@Test
void should_use_default_error_message_and_code_for_other_kc_exceptions() throws IOException {
void should_allow_uncaught_kc_exceptions_to_bubble_up() {
var handler = new ExecutionExceptionHandler();
int exitCode = handler.handleExecutionException(
new KafkaConnectException("Woa", 999), null, null);

assertThat(exitCode).isEqualTo(CommandLine.ExitCode.SOFTWARE);
assertThat(fakeSystemErr.toString())
.isEqualTo(errorPrintLnFormatted("An error occured."));
assertThrows(KafkaConnectException.class, () -> handler.handleExecutionException(
new KafkaConnectException("Woa", 999), null, null));
}

@Test
void should_use_default_error_message_and_code_for_non_kc_exceptions() throws IOException {
void should_use_default_error_message_and_code_for_non_kc_exceptions() {
var handler = new ExecutionExceptionHandler();
int exitCode = handler.handleExecutionException(
new Exception("Woa"), null, null);

assertThat(exitCode).isEqualTo(CommandLine.ExitCode.SOFTWARE);
assertThat(fakeSystemErr.toString())
.isEqualTo(errorPrintLnFormatted("An error occured."));
assertThrows(Exception.class, () -> handler.handleExecutionException(
new Exception("Woa"), null, null));
}
}
}

0 comments on commit 38e99a5

Please sign in to comment.