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

@ -72,19 +72,23 @@ import java.util.logging.Logger;
public final class EntryPoint {
private static final Logger LOGGER = Logger.getLogger("EntryPoint");
private EntryPoint() {
}
public static void main(String[] args) {
ExitCode exitCode = listen();
if (exitCode != ExitCode.NORMAL) {
LOGGER.warning("Exiting with " + exitCode);
//noinspection CallToSystemExit
System.exit(exitCode.numericalCode);
}
}
private static PreLaunchAction parseLine(String input, Parameters params) throws ParseException {
if (input.isEmpty())
throw new ParseException("Unexpected empty string!");
if (input.isEmpty()) // TODO: 2022-11-01 Should we just ignore this?
throw new ParseException("Unexpected empty string! You should not pass empty newlines to stdin.");
if ("launch".equalsIgnoreCase(input)) {
@ -94,7 +98,10 @@ public final class EntryPoint {
} else {
String[] pair = StringUtils.splitStringPair(' ', input);
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]);
@ -110,6 +117,7 @@ public final class EntryPoint {
String line;
while (preLaunchAction == PreLaunchAction.PROCEED) {
//noinspection NestedAssignment
if ((line = reader.readLine()) != null) {
preLaunchAction = parseLine(line, parameters);
} else {
@ -117,16 +125,16 @@ public final class EntryPoint {
}
}
} 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
if (preLaunchAction == PreLaunchAction.ABORT) {
LOGGER.info("Launch aborted by the launcher.");
return ExitCode.ERROR;
return ExitCode.ABORT;
}
try {
@ -138,9 +146,13 @@ public final class EntryPoint {
} catch (IllegalArgumentException 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) {
LOGGER.log(Level.SEVERE, "Exception caught from launcher.", e);
LOGGER.log(Level.SEVERE, "Exception caught from launcher", e);
return ExitCode.ERROR;
}
@ -155,7 +167,10 @@ public final class EntryPoint {
private enum ExitCode {
NORMAL(0),
ERROR(1);
ABORT(1),
ERROR(2),
ILLEGAL_ARGUMENT(3),
REFLECTION_EXCEPTION(4);
private final int numericalCode;