Skip to content
Myr

use errors.Is() to check for errors (#1730)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Since go 1.13 you can wrap errors. This make it no longer possible to compare with `==`, instead you have to compare with `errors.Is()`. I noticed this problem because -h was no longer working after I stared wrapping the errors in my custom FlagErrorFunc function. Note that this is only a problem when a custom help flag is defined.

authored by Paul Holzinger · committed by GitHub · parent ea94a3d · Browse files · 2 files+34 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Paul Holzinger <pholzing@redhat.com>

Diff stats

2 files+34 added−1 removed

  • other:+34 added−1 removed

2 files+34 added−1 removed

modified:command_test.go+32 added
1721 unmodified lines
1722 t.Errorf("Expected %v, got %v", expected, got)1723 }1724}17251726func TestFlagErrorFuncHelp(t *testing.T) {1727 c := &Command{Use: "c", Run: emptyRun}1728 c.PersistentFlags().Bool("help", false, "help for c")1729 c.SetFlagErrorFunc(func(_ *Command, err error) error {1730 return fmt.Errorf("wrap error: %w", err)1731 })17321733 out, err := executeCommand(c, "--help")1734 if err != nil {1735 t.Errorf("--help should not fail: %v", err)1736 }17371738 expected := `Usage:1739 c [flags]17401741Flags:1742 --help help for c1743`1744 if out != expected {1745 t.Errorf("Expected: %v, got: %v", expected, out)1746 }17471748 out, err = executeCommand(c, "-h")1749 if err != nil {1750 t.Errorf("-h should not fail: %v", err)1751 }17521753 if out != expected {1754 t.Errorf("Expected: %v, got: %v", expected, out)1755 }1756}17571758// TestSortedFlags checks,1759// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.1760// Related to https://github.com/spf13/cobra/issues/404.1761func TestSortedFlags(t *testing.T) {
434 unmodified lines
modified:command.go+2 added-1 removed
16 unmodified lines
1718import (19 "bytes"20 "context"21 "errors"22 "fmt"23 "io"24 "os"25 "path/filepath"
964 unmodified lines
990 err = cmd.execute(flags)991 if err != nil {992 // Always show help if requested, even if SilenceErrors is in993 // effect993 if err == flag.ErrHelp {994 if errors.Is(err, flag.ErrHelp) {995 cmd.HelpFunc()(cmd, args)996 return cmd, nil997 }998
698 unmodified lines