Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Error and exist code when using pkg/errors standard package #632

Closed
ehernandez-xk opened this issue May 17, 2017 · 4 comments
Closed

Comments

@ehernandez-xk
Copy link

Hi All,

Here is the snippet to reproduce the error

package main

import (
	"errors"

	"github.com/urfave/cli"
)

var testCMD = cli.Command{
	Name: "test",
	Action: func(c *cli.Context) error {
		return errors.New("Error detected")
	},
}

Command line: (The error is not printed)

$ myApp test
$ echo $?
0

This issue was added in 9e5b048 commit
and mentioned in #496 PR

By the way, Thanks for this awesome package.

@ehernandez-xk ehernandez-xk changed the title No Error and exist code when I use pkg/errors standard package No Error and exist code when using pkg/errors standard package May 17, 2017
@ehernandez-xk
Copy link
Author

ehernandez-xk commented May 17, 2017

I have noticed that only the errors of type ExitCoder interface are managed and the errors from the standard library are ignored

For example:

  return cli.NewExitError("Error message", 1)
  err := errors.New("errors")
  return cli.NewMultiError(err)

I think the standard errors should be used as well

@wenchma
Copy link

wenchma commented Jul 12, 2017

I encountered this issue in mac system, but it is OK in linux system.

@jszwedko
Copy link
Contributor

Hi @ehernandez-xk,

Apologies for the delay. #496 added functionality which was later deemed to be backwards incompatible and less flexible for users of the library, backed out in #595. Presently, if you want to have the application exit automatically, you should use cli.NewExitError as you note.

The error is bubbled up so you can still exit at the top-level if desired with something like:

package main                         

import (                             
        "errors"                     
        "fmt"                        
        "os"                         

        "github.com/urfave/cli"      
)                                    

var testCMD = cli.Command{           
        Name: "test",                
        Action: func(c *cli.Context) error {                               
                return errors.New("Error detected")                        
        },                           
}                                    

func main() {                        
        app := cli.NewApp()          

        app.Commands = []cli.Command{testCMD}                              

        err := app.Run(os.Args)      
        if err != nil {              
                fmt.Println(err)     
                os.Exit(1)           
        }                            
} 

app.RunAndExitOnError is provided as a convenience wrapper to automatically do this.

Let me know if this helps!

@ehernandez-xk
Copy link
Author

Thank you @jszwedko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants