Skip to content

Commit

Permalink
Better error output and exit code (#58)
Browse files Browse the repository at this point in the history
* Set SilenceErrors, SilenceUsage value

* return lastErr without usage

* Move error output position

* Fix test

* Add "Error: " in error output
  • Loading branch information
toshimaru committed Oct 31, 2019
1 parent ecba59a commit 607496e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ var rootCmd = &cobra.Command{
Example: `$ nyan FILE
$ nyan FILE1 FILE2 FILE3
$ nyan -t solarized-dark FILE`,
RunE: cmdMain,
RunE: cmdMain,
SilenceErrors: true,
SilenceUsage: false,
}

func init() {
Expand All @@ -46,8 +48,6 @@ func init() {

func main() {
if err := rootCmd.Execute(); err != nil {
// FIXME: use PrintErrln after upstream is fixed
rootCmd.PrintErr(err, "\n")
os.Exit(1)
}
}
Expand All @@ -66,6 +66,7 @@ func cmdMain(cmd *cobra.Command, args []string) (err error) {

if len(args) < 1 || args[0] == "-" {
if data, err = ioutil.ReadAll(cmd.InOrStdin()); err != nil {
cmd.PrintErr("Error: ", err, "\n")
return
}
if language != "" {
Expand All @@ -75,10 +76,12 @@ func cmdMain(cmd *cobra.Command, args []string) (err error) {
}
printData(&data, cmd, lexer)
} else {
var lastErr error
for _, filename := range args {
if data, err = ioutil.ReadFile(filename); err != nil {
// FIXME: use PrintErrln after upstream is fixed
cmd.PrintErr(err, "\n")
cmd.PrintErr("Error: ", err, "\n")
lastErr = err
continue
}
if language != "" {
Expand All @@ -88,8 +91,11 @@ func cmdMain(cmd *cobra.Command, args []string) (err error) {
}
printData(&data, cmd, lexer)
}
if lastErr != nil {
cmd.SilenceUsage = true
return lastErr
}
}

return
}

Expand Down
11 changes: 5 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func TestInvalidFilename(t *testing.T) {
rootCmd.SetErr(&e)
err := rootCmd.Execute()

assert.NotNil(t, err)
assert.NotNil(t, o.String())
assert.Contains(t, o.String(), "")
assert.Error(t, err)
assert.Empty(t, o.String())
assert.Contains(t, e.String(), invalidFileErrorMsg())
}

Expand Down Expand Up @@ -136,7 +135,7 @@ func TestMultipleFilesWithInvalidFileError(t *testing.T) {
rootCmd.SetErr(&e)
err := rootCmd.Execute()

assert.Nil(t, err)
assert.Error(t, err)
assert.NotNil(t, o.String())
assert.Contains(t, o.String(), highlightedGoCode)
assert.Contains(t, o.String(), "This is dummy.")
Expand Down Expand Up @@ -326,9 +325,9 @@ func resetStrings() {

func invalidFileErrorMsg() string {
if runtime.GOOS == "windows" {
return "open InvalidFilename: The system cannot find the file specified."
return "Error: open InvalidFilename: The system cannot find the file specified."
}
return "open InvalidFilename: no such file or directory"
return "Error: open InvalidFilename: no such file or directory"
}

func _unhighlightedGoCode() string {
Expand Down

0 comments on commit 607496e

Please sign in to comment.