Commit 25c1d543 authored by Matteo Melli's avatar Matteo Melli

Unique use of System.exit to end program.

parent 3d74a2fb
......@@ -29,7 +29,10 @@ import com.ongres.pgio.main.stats.StatSnapshot;
import com.ongres.pgio.main.stats.serializer.CsvSerializer;
import com.ongres.pgio.main.stats.serializer.StatSerializer;
import com.ongres.pgio.main.version.Version;
import fi.iki.elonen.NanoHTTPD;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
......@@ -53,19 +56,35 @@ import javax.json.JsonString;
public class Main {
public static void main(String[] args) throws Exception {
System.exit(run(args));
}
private static int run(String[] args) throws Exception {
OptionParser parser;
OptionSet options;
try {
parser = createOptionParser();
options = parser.parse(args);
} catch (OptionException ex) {
System.err.println(messageOrType(ex));
return -1;
} catch (RuntimeException ex) {
onException(ex, true);
return -1;
} catch (Exception ex) {
onException(ex, true);
return -1;
}
try {
OptionParser parser = createOptionParser();
OptionSet options = parser.parse(args);
if (options.has("help")) {
parser.printHelpOn(System.out);
System.exit(0);
return 0;
}
if (options.has("version")) {
System.out.println(Version.getVersion());
System.exit(0);
return 0;
}
Config config = configOptionSet(options);
......@@ -75,21 +94,47 @@ public class Main {
} else {
runCollector(config);
}
} catch (Throwable throwable) {
if (throwable.getMessage() != null) {
System.err.println(throwable.getMessage());
} else {
System.err.println("Seems like you hit a bug. Please open an issue with"
+ " the following stack trace at https://gitlab.com/teoincontatto/pgio/issues/new");
System.err.println();
throwable.printStackTrace(System.err);
System.err.println();
}
System.err.println("Try \"pgio --help\" for more information.");
System.exit(1);
} catch (RuntimeException ex) {
return onException(ex, options.has("debug"));
} catch (Exception ex) {
return onException(ex, options.has("debug"));
}
return 0;
}
private static int onException(Exception ex, boolean debug) {
System.err.println(messageOrType(ex));
if (debug) {
onExceptionHint(ex);
}
return -1;
}
private static void onExceptionHint(Exception ex) {
System.err.println();
System.err.println();
System.err.println("If you think you hit a bug. Please open an issue attaching"
+ " the following stack trace at https://gitlab.com/ongresinc/pgio/issues/new");
System.err.println();
ex.printStackTrace(System.err);
System.err.println();
System.err.println("Try \"pgio --help\" for more information.");
System.err.println();
}
private static String messageOrType(Exception ex) {
String message = ex.getMessage();
if (message == null) {
return ex.getClass().getName();
}
return message;
}
private static void runPrometheusService(Config config) throws IOException {
PrometheusService service = new PrometheusService(config);
service.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment