Skip to content

Commit

Permalink
Use correct error for arg errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudingcity committed Mar 8, 2020
1 parent 09a24b9 commit 45e30a7
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/ci_lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var ciLintCmd = &cobra.Command{
Short: "Validate the .gitlab-ci.yml",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a file")}
return &errs.ArgError{Err: errors.New("requires a file")}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var cloneCmd = &cobra.Command{
Short: "Clone a repository from GitLab",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a repository")}
return &errs.ArgError{Err: errors.New("requires a repository")}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/depend_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var dependGOCmd = &cobra.Command{
Short: "List go module package (example.com/hello) dependency",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a package")}
return &errs.ArgError{Err: errors.New("requires a package")}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/depend_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var dependPHPCmd = &cobra.Command{
Short: "List composer package (vendor/name) dependency",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a package")}
return &errs.ArgError{Err: errors.New("requires a package")}
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/mr_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var mrOpenCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("missing MRID")}
return &errs.ArgError{Err: errors.New("missing MRID")}
}
if _, err := strconv.Atoi(args[0]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var mrSearchCmd = &cobra.Command{
Short: "Search merge requests",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a query")}
return &errs.ArgError{Err: errors.New("requires a query")}
}
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var mrShowCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("missing MRID")}
return &errs.ArgError{Err: errors.New("missing MRID")}
}
if _, err := strconv.Atoi(args[0]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])}
}
return nil
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/own_mr_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ var ownMrOpenCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("missing MRID")}
return &errs.ArgError{Err: errors.New("missing MRID")}
}
if _, err := strconv.Atoi(args[0]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])}
}
if len(args) < 2 {
return &errs.FlagError{Err: errors.New("missing PID")}
return &errs.ArgError{Err: errors.New("missing PID")}
}
if _, err := strconv.Atoi(args[1]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid PID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid PID %q", args[0])}
}
return nil
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/own_mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ var ownMrShowCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("missing MRID")}
return &errs.ArgError{Err: errors.New("missing MRID")}
}
if _, err := strconv.Atoi(args[0]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid MRID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid MRID %q", args[0])}
}
if len(args) < 2 {
return &errs.FlagError{Err: errors.New("missing PID")}
return &errs.ArgError{Err: errors.New("missing PID")}
}
if _, err := strconv.Atoi(args[1]); err != nil {
return &errs.FlagError{Err: fmt.Errorf("invalid PID %q", args[0])}
return &errs.ArgError{Err: fmt.Errorf("invalid PID %q", args[0])}
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/project_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var projectSearchCmd = &cobra.Command{
Short: "Search projects",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return &errs.FlagError{Err: errors.New("requires a query")}
return &errs.ArgError{Err: errors.New("requires a query")}
}
return nil
},
Expand Down
19 changes: 17 additions & 2 deletions internal/errors/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@ func (e *FlagError) Unwrap() error {
return e.Err
}

type ArgError struct {
Err error
}

func (e *ArgError) Error() string {
return e.Err.Error()
}

func (e *ArgError) Unwrap() error {
return e.Err
}

func Handle(cmd *cobra.Command, err error) {
fmt.Println(err)

var flagError *FlagError
if errors.As(err, &flagError) || isUnknownCommand(err) || isDefaultFlagError(err) {
var (
flagError *FlagError
argError *ArgError
)
if errors.As(err, &flagError) || errors.As(err, &argError) || isUnknownCommand(err) || isDefaultFlagError(err) {
fmt.Println()
fmt.Println(cmd.UsageString())
}
Expand Down

0 comments on commit 45e30a7

Please sign in to comment.