Add more fine-grained exit codes

This commit is contained in:
Reinhard Pointner 2019-02-21 16:52:22 +07:00
parent 5402374d32
commit 164e6bc28e
5 changed files with 60 additions and 30 deletions

View File

@ -6,6 +6,7 @@ import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.Settings.*;
import static net.filebot.cli.ExitCode.*;
import static net.filebot.ui.GettingStartedUtil.*;
import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.FileUtilities.getChildren;
@ -63,12 +64,12 @@ public class Main {
// just print help message or version string and then exit
if (args.printHelp()) {
log.info(String.format("%s%n%n%s", getApplicationIdentifier(), args.usage()));
System.exit(0);
System.exit(SUCCESS);
}
if (args.printVersion()) {
log.info(String.join(" / ", getApplicationIdentifier(), getJavaRuntimeIdentifier(), getSystemIdentifier()));
System.exit(0);
System.exit(SUCCESS);
}
if (args.clearCache() || args.clearUserData()) {
@ -86,7 +87,7 @@ public class Main {
// clear cache must be called manually
if (System.console() == null) {
log.severe("`filebot -clear-cache` must be called from an interactive console.");
System.exit(1);
System.exit(ERROR);
}
log.info("Clear cache");
@ -97,7 +98,7 @@ public class Main {
}
// just clear cache and/or settings and then exit
System.exit(0);
System.exit(SUCCESS);
}
// make sure we can access application arguments at any time
@ -121,7 +122,7 @@ public class Main {
String psm = args.getLicenseKey();
if (psm != null) {
configureLicense(psm);
System.exit(0);
System.exit(SUCCESS);
}
}
@ -132,7 +133,7 @@ public class Main {
// just print help page if we can't run any command and also can't start the GUI
if (isHeadless()) {
log.info(String.format("%s / %s (headless)%n%n%s", getApplicationIdentifier(), getJavaRuntimeIdentifier(), args.usage()));
System.exit(1);
System.exit(ERROR);
}
// GUI mode => start user interface
@ -160,11 +161,11 @@ public class Main {
} catch (CmdLineException e) {
// illegal arguments => print CLI error message
log.severe(e::getMessage);
System.exit(1);
System.exit(ERROR);
} catch (Throwable e) {
// unexpected error => dump stack
debug.log(Level.SEVERE, "Error during startup", e);
System.exit(1);
System.exit(ERROR);
}
}

View File

@ -4,6 +4,7 @@ import static java.nio.charset.StandardCharsets.*;
import static net.filebot.Logging.*;
import static net.filebot.MediaTypes.*;
import static net.filebot.Settings.*;
import static net.filebot.cli.ExitCode.*;
import static net.filebot.util.ExceptionUtilities.*;
import static net.filebot.util.FileUtilities.*;
@ -37,9 +38,19 @@ public class ArgumentProcessor {
// script finished successfully
log.finest("Done ヾ(@⌒ー⌒@)");
return 0;
} catch (LicenseError e) {
log.severe("License Error: " + e.getMessage());
return SUCCESS;
} catch (Throwable e) {
int exitCode = getExitCode(e);
// script failed with exception -> exit with non-zero exit code (and use positive code to avoid issues with launch4j launcher)
log.finest("Failure (°_°)");
return exitCode;
}
}
private int getExitCode(Throwable e) {
if (findCause(e, LicenseError.class) != null) {
log.severe(message("License Error", e.getMessage()));
if (LICENSE.isFile()) {
printStegosaurus("Please purchase a FileBot License:", getPurchaseURL());
@ -50,20 +61,21 @@ public class ArgumentProcessor {
log.severe(format("FileBot requires a valid license. Please run `filebot --license *.psm` install your FileBot license."));
}
}
return 2;
} catch (Throwable e) {
if (findCause(e, CmdlineException.class) != null) {
log.log(Level.WARNING, findCause(e, CmdlineException.class).getMessage());
} else if (findCause(e, ScriptDeath.class) != null) {
log.log(Level.WARNING, findCause(e, ScriptDeath.class).getMessage());
} else {
log.log(Level.SEVERE, e.getMessage(), e);
}
// script failed with exception -> exit with non-zero exit code (and use positive code to avoid issues with launch4j launcher)
log.finest("Failure (°_°)");
return 1;
return BAD_LICENSE;
}
if (findCause(e, CmdlineException.class) != null) {
log.log(Level.WARNING, findCause(e, CmdlineException.class)::getMessage);
return FAILURE;
}
if (findCause(e, ScriptDeath.class) != null) {
ScriptDeath d = findCause(e, ScriptDeath.class);
log.log(Level.WARNING, d::getMessage);
return d.getExitCode();
}
return ERROR;
}
public int runCommand(CmdlineInterface cli, ArgumentBean args) throws Exception {
@ -121,14 +133,14 @@ public class ArgumentProcessor {
}
}
return 0;
return SUCCESS;
}
private int print(Stream<?> values) {
return values.mapToInt(v -> {
System.out.println(v);
return 1;
}).sum() == 0 ? 1 : 0;
return ERROR;
}).sum() == SUCCESS ? ERROR : SUCCESS;
}
private void printStegosaurus(String line1, String line2) {

View File

@ -0,0 +1,15 @@
package net.filebot.cli;
public class ExitCode {
public static final int SUCCESS = 0;
public static final int ERROR = 1;
public static final int BAD_LICENSE = 2;
public static final int FAILURE = 3;
public static final int DIE = 4;
}

View File

@ -14,4 +14,8 @@ public class ScriptDeath extends Throwable {
this.exitCode = exitCode;
}
public int getExitCode() {
return exitCode;
}
}

View File

@ -171,7 +171,7 @@ public abstract class ScriptShellBaseClass extends Script {
}
public void die(Object cause) throws Throwable {
die(EXIT_CODE_DIE, cause);
die(ExitCode.DIE, cause);
}
public void die(int exitCode, Object... cause) throws Throwable {
@ -571,6 +571,4 @@ public abstract class ScriptShellBaseClass extends Script {
return options.iterator().next();
}
public static final int EXIT_CODE_DIE = 4;
}