Skip to content

Commit

Permalink
Merge pull request #147 from planetscale/fatih/error-handling
Browse files Browse the repository at this point in the history
cmd: improved error handling to catch malformed responses
  • Loading branch information
fatih authored Apr 6, 2021
2 parents c680b9d + b60e0bf commit 148b58f
Show file tree
Hide file tree
Showing 37 changed files with 334 additions and 61 deletions.
9 changes: 7 additions & 2 deletions internal/cmd/backup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"
ps "github.com/planetscale/planetscale-go/planetscale"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,10 +38,14 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
defer end()
backup, err := client.Backups.Create(ctx, createReq)
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ func DeleteCmd(cfg *config.Config) *cobra.Command {
Backup: backup,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in branch %s of %s\n", cmdutil.BoldBlue(backup), cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

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

return nil
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/backup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(database))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return errors.Wrap(err, "error listing backups")
}
return errors.Wrap(err, "error listing backups")
}
end()

Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"
ps "github.com/planetscale/planetscale-go/planetscale"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -63,10 +64,14 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
defer end()
dbBranch, err := client.DatabaseBranches.Create(ctx, createReq)
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(source), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/branch/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ 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))
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(source), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/branch/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ func GetCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/branch/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Database: database,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(database), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return errors.Wrap(err, "error listing branches")
}
return errors.Wrap(err, "error listing branches")
}
end()

Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/branch/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ func StatusCmd(cfg *config.Config) *cobra.Command {
Branch: branch,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s", cmdutil.BoldBlue(branch), cmdutil.BoldBlue(source))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err

}

end()
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/database/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/printer"

"github.com/planetscale/planetscale-go/planetscale"
ps "github.com/planetscale/planetscale-go/planetscale"

"github.com/pkg/browser"
Expand Down Expand Up @@ -51,7 +52,14 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
defer end()
database, err := client.Databases.Create(ctx, createReq)
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in\n", cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

end()
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/database/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ func DeleteCmd(cfg *config.Config) *cobra.Command {
Database: name,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/database/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ func GetCmd(cfg *config.Config) *cobra.Command {
Database: name,
})
if err != nil {
if cmdutil.IsNotFoundError(err) {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n", cmdutil.BoldBlue(name), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
return err
}

end()
Expand Down
9 changes: 8 additions & 1 deletion internal/cmd/database/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Organization: cfg.Organization,
})
if err != nil {
return errors.Wrap(err, "error listing databases")
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist\n", cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return errors.Wrap(err, "error listing databases")
}
}

end()
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ func CloseCmd(cfg *config.Config) *cobra.Command {
Number: n,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s/%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(number), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

fmt.Printf("Deploy request %s/%s was successfully closed!\n",
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ func CreateCmd(cfg *config.Config) *cobra.Command {
IntoBranch: flags.deployTo,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}
end()

Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ func DeployCmd(cfg *config.Config) *cobra.Command {
Number: n,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s/%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(number), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

if cfg.OutputJSON {
Expand Down
14 changes: 11 additions & 3 deletions internal/cmd/deployrequest/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ func DiffCmd(cfg *config.Config) *cobra.Command {
Number: n,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s/%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(number), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

for _, df := range diffs {
Expand All @@ -62,9 +70,9 @@ func DiffCmd(cfg *config.Config) *cobra.Command {
for scanner.Scan() {
txt := scanner.Text()
if strings.HasPrefix(txt, "+") {
color.New(color.FgGreen).Add(color.Bold).Println(txt)
color.New(color.FgGreen).Add(color.Bold).Println(txt) //nolint: errcheck
} else if strings.HasPrefix(txt, "-") {
color.New(color.FgRed).Add(color.Bold).Println(txt)
color.New(color.FgRed).Add(color.Bold).Println(txt) //nolint: errcheck
} else {
fmt.Println(txt)
}
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ func ListCmd(cfg *config.Config) *cobra.Command {
Database: database,
})
if err != nil {
return errors.Wrap(err, "error listing deploy requests")
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return errors.Wrap(err, "error listing deploy requests")
}
}
end()

Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ func ReviewCmd(cfg *config.Config) *cobra.Command {
CommentText: flags.comment,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s/%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(number), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

switch action {
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/deployrequest/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ func ShowCmd(cfg *config.Config) *cobra.Command {
Number: n,
})
if err != nil {
return err
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("%s/%s does not exist in %s\n",
cmdutil.BoldBlue(database), cmdutil.BoldBlue(number), cmdutil.BoldBlue(cfg.Organization))
case planetscale.ErrResponseMalformed:
return cmdutil.MalformedError(err)
default:
return err
}
}

err = printer.PrintOutput(cfg.OutputJSON, printer.NewDeployRequestPrinter(dr))
Expand Down
Loading

0 comments on commit 148b58f

Please sign in to comment.