Skip to content

Commit

Permalink
Merge pull request #143 from planetscale/iheanyi/better-not-found-err…
Browse files Browse the repository at this point in the history
…ors-pt-1

Start on improving Not Found error messages
  • Loading branch information
iheanyi authored Apr 1, 2021
2 parents 5e66f90 + 6e3d8fc commit 8d7ad3f
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/cmd/backup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
defer end()
backup, err := client.Backups.Create(ctx, createReq)
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command {
Backup: backup,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in branch %s of %s\n", cmdutil.BoldBlue(backup), cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/backup/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func GetCmd(cfg *config.Config) *cobra.Command {
end()
err = printer.PrintOutput(cfg.OutputJSON, printer.NewBackupPrinter(b))
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in branch %s of %s\n", cmdutil.BoldBlue(backup), cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/backup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
}
return errors.Wrap(err, "error listing backups")
}
end()
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
defer end()
dbBranch, err := client.DatabaseBranches.Create(ctx, createReq)
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(source), cmdutil.BoldBlue(cfg.Organization))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func GetCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Database: database,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(database), cmdutil.BoldBlue(cfg.Organization))
}
return errors.Wrap(err, "error listing branches")
}
end()
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func StatusCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/database/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func DeleteCmd(cfg *config.Config) *cobra.Command {
Database: name,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization))
}
return err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/database/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func GetCmd(cfg *config.Config) *cobra.Command {
Database: name,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization))
}
return err
}

Expand Down
16 changes: 16 additions & 0 deletions internal/cmdutil/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/planetscale/planetscale-go/planetscale"
)

var IsTTY = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
Expand Down Expand Up @@ -39,12 +40,27 @@ func PrintProgress(message string) func() {

// BoldBlue returns a string formatted with blue and bold.
func BoldBlue(msg string) string {
if !IsTTY {
return msg
}
// the 'color' package already handles IsTTY gracefully
return color.New(color.FgBlue).Add(color.Bold).Sprint(msg)
}

// Bold returns a string formatted with bold.
func Bold(msg string) string {
if !IsTTY {
return msg
}
// the 'color' package already handles IsTTY gracefully
return color.New(color.Bold).Sprint(msg)
}

func IsNotFoundError(ogErr error) bool {
err, ok := ogErr.(*planetscale.ErrorResponse)
if ok {
return err.Code == "not_found"
}

return false
}

0 comments on commit 8d7ad3f

Please sign in to comment.