Cli: Make action functions explicitly require the ExitCoder interface instead of an error

Created on 20 Mar 2017  路  6Comments  路  Source: urfave/cli

The current release handles errors returned by the actions/subcommands, the current master branch ignores them. This behavior has flipped back and forth, but the current practice seems like the cli library will ignore errors that are returned by functions passed into it unless they match the ExitCoder interface.

If this is the way forward, make the functions check this at compile time instead of run time.

It seems completely unidiomatic for users of the api to expect errors they return from those functions to be silently ignored, so don't let them assume that they can pass errors back.

(As an aside, I think the whole ExitCoder thing should definitely be opt in and you should leave the behavior of returning errors and them being handled by the library as it is in the current release. But if you are going to go away from that behavior you should enforce it at compile time)

arev2 help wanted kinmaintenance statuconfirmed

All 6 comments

Should probably include something like this in the library as well

func WrapExitCoder(f func(*cli.Context) error) func(*cli.Context) cli.ExitCoder {
    return func(cliCtx *cli.Context) cli.ExitCoder {
        err := f(cliCtx)
        if err != nil {
            return cli.NewExitError(err, 1)
        }

        return nil
    }
}

Hi @kklipsch,

Thank you far raising this up!

As I mention in https://github.com/urfave/cli/issues/594#issuecomment-296829758; the error isn't silently ignored, but rather it is propagated up to the App.Run() method so that the caller can decide what do with it (RunAndExitOnError being a convenience wrapper for exiting if a non-ExitCoder error occurred). The ExitCoder interface was added to support functions returning the exit code to exit with closer to where the error occurred, but I can see an argument for still not automatically exiting and allowing the caller to assert on the type and decide how to handle it.

I'll mark this as something to reconsider as part of the v2 branch where we can make breaking changes.

馃憢 v2 just hit master, so we'll need to check if this is still a valid issue 馃攳

This is still a valid issue.

@Nokel81 how related is this to https://github.com/urfave/cli/issues/1088?

It is only tangentially related. Because v2 has already been released doing this should be part of a v3 since it is a breaking change.

It is related because it is part of the work required in moving more of urfave/cli errors to be ExitCoder

Was this page helpful?
0 / 5 - 0 ratings

Related issues

costela picture costela  路  3Comments

navono picture navono  路  4Comments

costela picture costela  路  5Comments

genieplus picture genieplus  路  5Comments

krostar picture krostar  路  5Comments