forked from kcctl/kcctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 kcctl#17 Added global execution exception handler to handle KafkaCo…
…nnectExceptions
- Loading branch information
1 parent
20758de
commit a9bb7b1
Showing
4 changed files
with
172 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/dev/morling/kccli/service/ExecutionExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2021 The original authors | ||
* | ||
* Licensed 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 dev.morling.kccli.service; | ||
|
||
import org.apache.http.HttpStatus; | ||
|
||
import dev.morling.kccli.util.Colors; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.IExecutionExceptionHandler; | ||
import picocli.CommandLine.ParseResult; | ||
|
||
public class ExecutionExceptionHandler implements IExecutionExceptionHandler { | ||
|
||
public static class ExitCodeErrorMessagePair { | ||
private int exitCode; | ||
private String errorMessage; | ||
|
||
public ExitCodeErrorMessagePair(int exitCode, String errorMessage) { | ||
this.exitCode = exitCode; | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
public int getExitCode() { | ||
return this.exitCode; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return this.errorMessage; | ||
} | ||
} | ||
|
||
@Override | ||
public int handleExecutionException(Exception ex, CommandLine commandLine, ParseResult parseResult) { | ||
var exitCodeErrorMessagePair = loadMessageAndExitCode(ex); | ||
System.err.println(Colors.ANSI_RED + exitCodeErrorMessagePair.getErrorMessage() + Colors.ANSI_RESET); | ||
return exitCodeErrorMessagePair.getExitCode(); | ||
} | ||
|
||
public static ExitCodeErrorMessagePair loadMessageAndExitCode(Exception ex) { | ||
if (ex instanceof KafkaConnectException) { | ||
var kafkaConnectException = (KafkaConnectException) ex; | ||
switch (kafkaConnectException.getErrorCode()) { | ||
case HttpStatus.SC_UNAUTHORIZED: { | ||
return new ExitCodeErrorMessagePair( | ||
CommandLine.ExitCode.SOFTWARE, | ||
"Kafka Connect returned an error indicating the configured user is unauthorized."); | ||
} | ||
} | ||
} | ||
|
||
return new ExitCodeErrorMessagePair(CommandLine.ExitCode.SOFTWARE, "An error occured."); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/test/java/dev/morling/kccli/service/ExecutionExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2021 The original authors | ||
* | ||
* Licensed 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 dev.morling.kccli.service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import dev.morling.kccli.util.Colors; | ||
import picocli.CommandLine; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DisplayNameGeneration(ReplaceUnderscores.class) | ||
class ExecutionExceptionHandlerTest { | ||
@TempDir | ||
File tempDir; | ||
|
||
@Nested | ||
class HandleExecutionException { | ||
// Thanks to https://stackoverflow.com/a/1119559 | ||
private final ByteArrayOutputStream fakeSystemErr = new ByteArrayOutputStream(); | ||
private final PrintStream originalSystemErr = System.err; | ||
|
||
@BeforeEach | ||
public void setupSystemErr() { | ||
System.setErr(new PrintStream(fakeSystemErr)); | ||
} | ||
|
||
@AfterEach | ||
public void restoreSystemErr() { | ||
System.setErr(originalSystemErr); | ||
} | ||
|
||
public String errorPrintLnFormatted(String str) { | ||
return Colors.ANSI_RED + str + Colors.ANSI_RESET + System.getProperty("line.separator"); | ||
} | ||
|
||
@Test | ||
void should_handle_unauthorized_errors() throws IOException { | ||
var handler = new ExecutionExceptionHandler(); | ||
int exitCode = handler.handleExecutionException( | ||
new KafkaConnectException("Unauthorized", 401), null, null); | ||
|
||
assertThat(exitCode).isEqualTo(CommandLine.ExitCode.SOFTWARE); | ||
assertThat(fakeSystemErr.toString()) | ||
.isEqualTo(errorPrintLnFormatted( | ||
"Kafka Connect returned an error indicating the configured user is unauthorized.")); | ||
} | ||
|
||
@Test | ||
void should_use_default_error_message_and_code_for_other_kc_exceptions() throws IOException { | ||
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.")); | ||
} | ||
|
||
@Test | ||
void should_use_default_error_message_and_code_for_non_kc_exceptions() throws IOException { | ||
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.")); | ||
} | ||
} | ||
} |