Skip to content

Commit

Permalink
show command usage only if the error is of type ArgumentError
Browse files Browse the repository at this point in the history
And if it is cobra error (which is hard to detect because it is not
typed).
Trying to allow usage of application errors insead of wrapping all in UserMessage.
  • Loading branch information
ianic committed Oct 19, 2021
1 parent 91990eb commit db7d390
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
4 changes: 3 additions & 1 deletion cli/cmd/examples/errors_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package examples
import (
"fmt"

"github.com/mantil-io/mantil/cli/controller"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/cli/ui"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,7 +32,8 @@ type errStack struct{}

func (e *errStack) run(pero string) error {
if pero == "zdero" { // this will show error and command usage!
return log.Wrap(fmt.Errorf("pero ne moze biti zdero"))
//return log.Wrap(fmt.Errorf("pero ne moze biti zdero"))
return log.Wrap(controller.NewArgumentError("pero ne moze biti zdero"))
}
ui.Info("in run")
return e.first()
Expand Down
21 changes: 18 additions & 3 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package cmd

import (
"github.com/mantil-io/mantil/cli/build"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/cli/controller"
"github.com/mantil-io/mantil/cli/ui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
Expand All @@ -14,10 +15,24 @@ func Execute() error {
return nil
}

ui.Error(err) // this will handle UserError case
if !log.IsUserError(err) { // show usage for cobar errors, and other usage errors
// show usage in case of ArgumentError
var ae *controller.ArgumentError
if errors.As(err, &ae) {
ui.Error(ae)
ec.Usage()
return err
}

// if the error is not wrapped then it is probably generated by cobra
// show usage because that is breaking some arguments constraints
if errors.Unwrap(err) == nil {
ui.Error(err)
ec.Usage()
return err
}

// in other cases show error without usage
ui.Error(err)
return err
}

Expand Down
19 changes: 14 additions & 5 deletions cli/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import (
"github.com/mantil-io/mantil/workspace"
)

type ArgumentError struct {
msg string
}

func (a *ArgumentError) Error() string {
return a.msg
}

func NewArgumentError(format string, v ...interface{}) *ArgumentError {
msg := fmt.Sprintf(format, v...)
return &ArgumentError{msg: msg}
}

func AWSClient(account *workspace.Account, project *workspace.Project, stage *workspace.Stage) (*aws.AWS, error) {
restEndpoint := account.Endpoints.Rest

Expand Down Expand Up @@ -80,17 +93,13 @@ func InvokeCallback(stage *workspace.Stage, path, req string, includeHeaders, in

// ensures that workspace and project exists
func NewStore() (*workspace.FileStore, error) {
noProjectFoundMsg := "Mantil project not found"
fs, err := workspace.NewSingleDeveloperProjectStore()
if err != nil {
if err == workspace.ErrProjectNotFound {
return nil, log.WithUserMessage(err, noProjectFoundMsg)
}
return nil, log.Wrap(err)
}
project := fs.Project()
if project == nil {
return nil, log.WithUserMessage(nil, noProjectFoundMsg)
return nil, workspace.ErrProjectNotFound
}
return fs, nil
}
Expand Down
12 changes: 6 additions & 6 deletions cli/controller/setup_args.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package controller

import (
"fmt"
"os"

"github.com/mantil-io/mantil/aws"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/workspace"
)

Expand Down Expand Up @@ -46,25 +46,25 @@ func (a *SetupArgs) validate() error {
if a.Profile != "" {
return nil
}
return fmt.Errorf("AWS credentials not provided")
return log.Wrap(NewArgumentError("AWS credentials not provided"))
}

func (a *SetupArgs) validateAccessKeys() error {
if a.AccessKeyID == "" {
return fmt.Errorf("aws-access-key-id not provided, must be used with the aws-secret-access-key and aws-region")
return log.Wrap(NewArgumentError("aws-access-key-id not provided, must be used with the aws-secret-access-key and aws-region"))
}
if a.SecretAccessKey == "" {
return fmt.Errorf("aws-secret-access-key not provided, must be used with the aws-access-key-id and aws-region")
return log.Wrap(NewArgumentError("aws-secret-access-key not provided, must be used with the aws-access-key-id and aws-region"))
}
if a.Region == "" {
return fmt.Errorf("aws-region not provided, must be used with aws-access-key-id and aws-secret-access-key")
return log.Wrap(NewArgumentError("aws-region not provided, must be used with aws-access-key-id and aws-secret-access-key"))
}
return nil
}

func (a *SetupArgs) readFromEnv() error {
errf := func(env string) error {
return fmt.Errorf("%s environment variable not provided", env)
return NewArgumentError("%s environment variable not provided", env)
}
a.AccessKeyID, _ = os.LookupEnv(accessKeyIDEnv)
if a.AccessKeyID == "" {
Expand Down
2 changes: 1 addition & 1 deletion cli/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (u *Logger) Error(err error) {
}
msg := err.Error()
log.PrintfWithCallDepth(2, "[cli.Error] %s", msg)
u.Errorf(msg)
u.Errorf("Error: %s", msg)
}

func (u *Logger) Errorf(format string, v ...interface{}) {
Expand Down

0 comments on commit db7d390

Please sign in to comment.