Telenav / kivakit

KivaKit is a set of integrated Java mini-frameworks for everyday development.
Apache License 2.0
83 stars 10 forks source link

Failure gets dropped on floor during app initialization #108

Closed timboudreau closed 2 years ago

timboudreau commented 2 years ago
            SwitchParser<String> bazSwitch = SwitchParser.builder(String.class)
                    .name("baz")
                    .defaultValue("wookie")
//                    .description("A thing that is a thing")
                    .optional()
//                    .converter(new BaseStringConverter<String>(this, str -> str)
//                    {
//                    })
                    .build();

Failure should probably throw an exception if nothing handles or is listening to the failure. Or return a FailureResult that lets the application decide to do that.

jonathanl-telenav commented 2 years ago

So an exception is being thrown by a field initializer of your application?

The run() method for application handles failures, but construction and execution of an Application is two-phase right now, like:

    public static void main(String[] arguments)
    {
        new MyApplication().run(arguments);
    }

The only way I can think of to really make sure the main() method captures the failure is to have construction and running of the application performed by KivaKit, like:

    public static void main(String[] arguments)
    {
        Application.run(MyApplication.class);
    }
jonathanl-telenav commented 2 years ago

I'm not able to build right now, so I'm not positive that these changes work, but the idea is that this method

    public static <T extends Application> Result<ApplicationExit> run(Listener listener,
                                                                      Class<T> applicationType,
                                                                      String[] arguments)

can be used to create the application and run it in one shot. This way construction and execution are scoped properly, and all exceptions are caught within the method. The Result object contains an ApplicationExit value and any messages that were caught. The ApplicationExit object contains any exception and an enum ExitCode (either success or failure as of now).

jonathanl-telenav commented 2 years ago

PR is here: https://github.com/Telenav/kivakit/pull/119