Skip to content

Commit

Permalink
Make debug text platform independent and flush error output stream in…
Browse files Browse the repository at this point in the history
… exception handler
  • Loading branch information
baloo42 committed Oct 24, 2024
1 parent 7c5ccbe commit 7d303f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,43 @@ CRDGenerationInfo getCrdGenerationInfo() {
return crdGenerationInfo;
}

String getDiagText() {
/**
* Get details as text about the internal state.
*
* @return the debug text.
*/
String getDebugText() {
final String lineSeparator = System.lineSeparator();
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append(lineSeparator);

if (!customResourceClassNames.isEmpty()) {
sb.append("Custom Resource class names:\n");
customResourceClassNames.forEach(fqcn -> sb.append(" ").append(fqcn).append("\n"));
sb.append("Custom Resource class names:");
sb.append(lineSeparator);
customResourceClassNames.forEach(fqcn -> sb.append(" ").append(fqcn).append(lineSeparator));
sb.append(lineSeparator);
}
if (filesToScan.isEmpty()) {
sb.append("Scan Paths: []\n");
sb.append("Scan Paths: []");
sb.append(lineSeparator);
} else {
sb.append("Scan Paths:\n");
filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append("\n"));
sb.append("Scan Paths:");
sb.append(lineSeparator);
filesToScan.forEach(f -> sb.append(" ").append(f.getPath()).append(lineSeparator));
sb.append(lineSeparator);
}

List<String> allClasspathElements = getClasspathElements();
if (allClasspathElements.isEmpty()) {
sb.append("Classpath: []\n");
sb.append("Classpath: []");
sb.append(lineSeparator);
} else {
sb.append("\nClasspath:\n");
allClasspathElements.forEach(cpe -> sb.append(" ").append(cpe).append("\n"));
sb.append("Classpath:");
sb.append(lineSeparator);
allClasspathElements.forEach(cpe -> sb.append(" ").append(cpe).append(lineSeparator));
sb.append(lineSeparator);
}
sb.append("\n");
sb.append(lineSeparator);
return sb.toString();
}

Expand Down Expand Up @@ -311,7 +325,7 @@ static CommandLine createCommandLine() {

static CommandLine createCommandLine(CRDGeneratorCLI crdGeneratorCLI) {
return new CommandLine(crdGeneratorCLI)
.setExecutionExceptionHandler(new CRDGeneratorExecutionExceptionHandler(crdGeneratorCLI::getDiagText));
.setExecutionExceptionHandler(new CRDGeneratorExecutionExceptionHandler(crdGeneratorCLI::getDebugText));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class CRDGeneratorExecutionExceptionHandler implements CommandLine.IExecutionExc

private static final Logger log = LoggerFactory.getLogger(CRDGeneratorExecutionExceptionHandler.class);

private final Supplier<String> diagTextSupplier;
private final Supplier<String> debugTextSupplier;

CRDGeneratorExecutionExceptionHandler(Supplier<String> diagTextSupplier) {
this.diagTextSupplier = diagTextSupplier;
CRDGeneratorExecutionExceptionHandler(Supplier<String> debugTextSupplier) {
this.debugTextSupplier = debugTextSupplier;
}

@Override
Expand All @@ -42,11 +42,13 @@ public int handleExecutionException(

if (ex instanceof CustomResourceClassLoaderException) {
commandLine.getErr().println();
commandLine.getErr().println("The classloader could not load the Custom Resource class.\n" +
commandLine.getErr().println("The classloader could not load the Custom Resource class.");
commandLine.getErr().println(
"Check the list of classpath elements and add further JAR archives " +
"or directories containing required classes " +
"e.g. with `-cp my-dep.jar` or `-cp target/classes/`.");
commandLine.getErr().print(diagTextSupplier.get());
"or directories containing required classes " +
"e.g. with `-cp my-dep.jar` or `-cp target/classes/`.");
commandLine.getErr().print(debugTextSupplier.get());
commandLine.getErr().flush();
return CRDGeneratorExitCode.CR_CLASS_LOADING;
}

Expand All @@ -55,15 +57,17 @@ public int handleExecutionException(
commandLine.getErr().println("Check JAR files and directories considered to be scanned " +
"as well as your filters. At least one Custom Resource class " +
"must be retained after filtering.");
commandLine.getErr().print(diagTextSupplier.get());
commandLine.getErr().print(debugTextSupplier.get());
commandLine.getErr().flush();
return CRDGeneratorExitCode.NO_CR_CLASSES_RETAINED;
}

if (log.isDebugEnabled()) {
commandLine.getErr().println(diagTextSupplier.get());
commandLine.getErr().println(debugTextSupplier.get());
}

log.trace(ex.getMessage(), ex);
commandLine.getErr().flush();
return CRDGeneratorExitCode.SOFTWARE;
}
}

0 comments on commit 7d303f8

Please sign in to comment.