Skip to content

Commit

Permalink
Make commange error handling consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
optik-aper committed Mar 4, 2024
1 parent b219d1c commit 0cb7c8f
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 337 deletions.
10 changes: 8 additions & 2 deletions cmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package account
import (
"context"
"errors"
"fmt"

"github.com/spf13/cobra"
"github.com/vultr/govultr/v3"
Expand Down Expand Up @@ -34,10 +35,15 @@ func NewCmdAccount(base *cli.Base) *cobra.Command {
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
account, err := o.get()
if err != nil {
return fmt.Errorf("error retrieving account information : %v", err)
}

o.Base.Printer.Display(&AccountPrinter{Account: account}, nil)

o.Base.Printer.Display(&AccountPrinter{Account: account}, err)
return nil
},
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/backups/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ package backups
import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/vultr/govultr/v3"
"github.com/vultr/vultr-cli/v3/cmd/printer"
"github.com/vultr/vultr-cli/v3/cmd/utils"
"github.com/vultr/vultr-cli/v3/pkg/cli"
)
Expand Down Expand Up @@ -48,15 +46,16 @@ func NewCmdBackups(base *cli.Base) *cobra.Command {
Aliases: []string{"l"},
Long: listLong,
Example: listExample,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
o.Base.Options = utils.GetPaging(cmd)
backups, meta, err := o.list()
if err != nil {
printer.Error(fmt.Errorf("error retrieving backups list : %v", err))
os.Exit(1)
return fmt.Errorf("error retrieving backups list : %v", err)
}
data := &BackupsPrinter{Backups: backups, Meta: meta}
o.Base.Printer.Display(data, err)

return nil
},
}

Expand All @@ -80,14 +79,16 @@ func NewCmdBackups(base *cli.Base) *cobra.Command {
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
backup, err := o.get()
if err != nil {
panic(fmt.Errorf("error retrieving backup : %v", err))
return fmt.Errorf("error retrieving backup : %v", err)
}

data := &BackupPrinter{Backup: backup}
o.Base.Printer.Display(data, err)

return nil
},
}

Expand Down
Loading

0 comments on commit 0cb7c8f

Please sign in to comment.