More informative exceptions in entrypoint

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2022-11-01 12:34:13 -04:00 committed by TheKodeToad
parent dabb84f62a
commit e899e31745

View File

@ -71,22 +71,26 @@ import java.util.logging.Logger;
public final class EntryPoint { public final class EntryPoint {
private static final Logger LOGGER = Logger.getLogger("EntryPoint"); private static final Logger LOGGER = Logger.getLogger("EntryPoint");
private EntryPoint() {
}
public static void main(String[] args) { public static void main(String[] args) {
ExitCode exitCode = listen(); ExitCode exitCode = listen();
if (exitCode != ExitCode.NORMAL) { if (exitCode != ExitCode.NORMAL) {
LOGGER.warning("Exiting with " + exitCode); LOGGER.warning("Exiting with " + exitCode);
//noinspection CallToSystemExit
System.exit(exitCode.numericalCode); System.exit(exitCode.numericalCode);
} }
} }
private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException { private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException {
if (input.isEmpty()) if (input.isEmpty()) // TODO: 2022-11-01 Should we just ignore this?
throw new ParseException("Unexpected empty string!"); throw new ParseException("Unexpected empty string! You should not pass empty newlines to stdin.");
if ("launch".equalsIgnoreCase(input)) { if ("launch".equalsIgnoreCase(input)) {
return PreLaunchAction.LAUNCH; return PreLaunchAction.LAUNCH;
} else if ("abort".equalsIgnoreCase(input)) { } else if ("abort".equalsIgnoreCase(input)) {
@ -94,22 +98,26 @@ public final class EntryPoint {
} else { } else {
String[] pair = StringUtils.splitStringPair(' ', input); String[] pair = StringUtils.splitStringPair(' ', input);
if (pair == null) if (pair == null)
throw new ParseException("Error while parsing:" + input); throw new ParseException(String.format(
"Could not split input string '%s' by space. All input provided from stdin must be either 'launch', 'abort', or " +
"in the format '[param name] [param]'.",
input));
params.add(pair[0], pair[1]); params.add(pair[0], pair[1]);
return PreLaunchAction.PROCEED; return PreLaunchAction.PROCEED;
} }
} }
private static ExitCode listen() { private static ExitCode listen() {
Parameters parameters = new Parameters(); Parameters parameters = new Parameters();
PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED; PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) {
String line; String line;
while (preLaunchAction == PreLaunchAction.PROCEED) { while (preLaunchAction == PreLaunchAction.PROCEED) {
//noinspection NestedAssignment
if ((line = reader.readLine()) != null) { if ((line = reader.readLine()) != null) {
preLaunchAction = parseLine(line, parameters); preLaunchAction = parseLine(line, parameters);
} else { } else {
@ -117,51 +125,58 @@ public final class EntryPoint {
} }
} }
} catch (IOException | ParseException e) { } catch (IOException | ParseException e) {
LOGGER.log(Level.SEVERE, "Launcher abort due to exception:", e); LOGGER.log(Level.SEVERE, "Launcher abort due to exception", e);
return ExitCode.ERROR; return ExitCode.ILLEGAL_ARGUMENT;
} }
// Main loop // Main loop
if (preLaunchAction == PreLaunchAction.ABORT) { if (preLaunchAction == PreLaunchAction.ABORT) {
LOGGER.info("Launch aborted by the launcher."); LOGGER.info("Launch aborted by the launcher.");
return ExitCode.ERROR; return ExitCode.ABORT;
} }
try { try {
Launcher launcher = LauncherFactory.createLauncher(parameters); Launcher launcher = LauncherFactory.createLauncher(parameters);
launcher.launch(); launcher.launch();
return ExitCode.NORMAL; return ExitCode.NORMAL;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOGGER.log(Level.SEVERE, "Wrong argument.", e); LOGGER.log(Level.SEVERE, "Wrong argument.", e);
return ExitCode.ERROR; return ExitCode.ILLEGAL_ARGUMENT;
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
LOGGER.log(Level.SEVERE, "Caught reflection exception from launcher", e);
return ExitCode.REFLECTION_EXCEPTION;
} catch (Throwable e) { } catch (Throwable e) {
LOGGER.log(Level.SEVERE, "Exception caught from launcher.", e); LOGGER.log(Level.SEVERE, "Exception caught from launcher", e);
return ExitCode.ERROR; return ExitCode.ERROR;
} }
} }
private enum PreLaunchAction { private enum PreLaunchAction {
PROCEED, PROCEED,
LAUNCH, LAUNCH,
ABORT ABORT
} }
private enum ExitCode { private enum ExitCode {
NORMAL(0), NORMAL(0),
ERROR(1); ABORT(1),
ERROR(2),
ILLEGAL_ARGUMENT(3),
REFLECTION_EXCEPTION(4);
private final int numericalCode; private final int numericalCode;
ExitCode(int numericalCode) { ExitCode(int numericalCode) {
this.numericalCode = numericalCode; this.numericalCode = numericalCode;
} }
} }
} }