Skip to content

Commit

Permalink
improve GraphQL error handling (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz authored Nov 16, 2022
1 parent d9975f1 commit 16230fc
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 57 deletions.
18 changes: 9 additions & 9 deletions cmd/plural/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (p *Plural) handleInstallations(c *cli.Context) error {
p.InitPluralClient()
installations, err := p.GetInstallations()
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallations")
}

installations = algorithms.Filter(installations, func(v *api.Installation) bool {
Expand All @@ -100,7 +100,7 @@ func (p *Plural) handleCharts(c *cli.Context) error {
p.InitPluralClient()
charts, err := p.GetCharts(c.Args().First())
if err != nil {
return err
return api.GetErrorResponse(err, "GetCharts")
}

headers := []string{"Id", "Name", "Description", "Latest Version"}
Expand All @@ -113,7 +113,7 @@ func (p *Plural) handleTerraforma(c *cli.Context) error {
p.InitPluralClient()
tfs, err := p.GetTerraforma(c.Args().First())
if err != nil {
return err
return api.GetErrorResponse(err, "GetTerraforma")
}

headers := []string{"Id", "Name", "Description"}
Expand All @@ -125,9 +125,8 @@ func (p *Plural) handleTerraforma(c *cli.Context) error {
func (p *Plural) handleVersions(c *cli.Context) error {
p.InitPluralClient()
versions, err := p.GetVersions(c.Args().First())

if err != nil {
return err
return api.GetErrorResponse(err, "GetVersions")
}

headers := []string{"Id", "Version"}
Expand All @@ -140,7 +139,7 @@ func (p *Plural) handleChartInstallations(c *cli.Context) error {
p.InitPluralClient()
chartInstallations, err := p.GetChartInstallations(c.Args().First())
if err != nil {
return err
return api.GetErrorResponse(err, "GetChartInstallations")
}

cis := algorithms.Filter(chartInstallations, func(ci *api.ChartInstallation) bool {
Expand All @@ -158,7 +157,7 @@ func (p *Plural) handleTerraformInstallations(c *cli.Context) error {
p.InitPluralClient()
terraformInstallations, err := p.GetTerraformInstallations(c.Args().First())
if err != nil {
return err
return api.GetErrorResponse(err, "GetTerraformInstallations")
}

tis := algorithms.Filter(terraformInstallations, func(ti *api.TerraformInstallation) bool {
Expand All @@ -176,7 +175,7 @@ func (p *Plural) handleArtifacts(c *cli.Context) error {
p.InitPluralClient()
artifacts, err := p.ListArtifacts(c.Args().First())
if err != nil {
return err
return api.GetErrorResponse(err, "ListArtifacts")
}

headers := []string{"Id", "Name", "Platform", "Blob", "Sha"}
Expand All @@ -187,5 +186,6 @@ func (p *Plural) handleArtifacts(c *cli.Context) error {

func (p *Plural) handleCreateDomain(c *cli.Context) error {
p.InitPluralClient()
return p.CreateDomain(c.Args().First())
err := p.CreateDomain(c.Args().First())
return api.GetErrorResponse(err, "CreateDomain")
}
4 changes: 2 additions & 2 deletions cmd/plural/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *Plural) bundleList(c *cli.Context) error {
p.InitPluralClient()
recipes, err := p.ListRecipes(repo, prov)
if err != nil {
return err
return api.GetErrorResponse(err, "ListRecipes")
}

headers := []string{"Name", "Description", "Provider", "Install Command"}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (p *Plural) stackList(c *cli.Context) (err error) {
p.InitPluralClient()
stacks, err := p.ListStacks(c.Bool("account"))
if err != nil {
return err
return api.GetErrorResponse(err, "ListStacks")
}

headers := []string{"Name", "Description", "Featured"}
Expand Down
16 changes: 8 additions & 8 deletions cmd/plural/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p *Plural) getSortedInstallations(repo string) ([]*api.Installation, error
p.InitPluralClient()
installations, err := p.GetInstallations()
if err != nil {
return installations, err
return installations, api.GetErrorResponse(err, "GetInstallations")
}

if len(installations) == 0 {
Expand All @@ -47,7 +47,7 @@ func (p *Plural) allSortedRepos() ([]string, error) {
p.InitPluralClient()
insts, err := p.GetInstallations()
if err != nil {
return nil, err
return nil, api.GetErrorResponse(err, "GetInstallations")
}

return wkspace.SortAndFilter(insts)
Expand Down Expand Up @@ -103,7 +103,7 @@ func (p *Plural) build(c *cli.Context) error {
if c.IsSet("only") {
installation, err := p.GetInstallation(c.String("only"))
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
} else if installation == nil {
return utils.HighlightError(fmt.Errorf("%s is not installed. Please install it with `plural bundle install`", c.String("only")))
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (p *Plural) validate(c *cli.Context) error {
if c.IsSet("only") {
installation, err := p.GetInstallation(c.String("only"))
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
}
return p.doValidate(installation)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (p *Plural) deploy(c *cli.Context) error {

installation, err := p.GetInstallation(repo)
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
}
if installation == nil {
return fmt.Errorf("The %s was unistalled, run `plural bundle install %s <bundle-name>` ", repo, repo)
Expand Down Expand Up @@ -327,7 +327,7 @@ func (p *Plural) bounce(c *cli.Context) error {
if repoName != "" {
installation, err := p.GetInstallation(repoName)
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
}
return p.doBounce(repoRoot, installation)
}
Expand Down Expand Up @@ -383,7 +383,7 @@ func (p *Plural) destroy(c *cli.Context) error {
if repoName != "" {
installation, err := p.GetInstallation(repoName)
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
}

return p.doDestroy(repoRoot, installation)
Expand Down Expand Up @@ -450,7 +450,7 @@ func (p *Plural) buildContext(_ *cli.Context) error {
p.InitPluralClient()
insts, err := p.GetInstallations()
if err != nil {
return err
return api.GetErrorResponse(err, "GetInstallation")
}

path := manifest.ContextPath()
Expand Down
8 changes: 4 additions & 4 deletions cmd/plural/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func handleLogin(c *cli.Context) error {

device, err := client.DeviceLogin()
if err != nil {
return err
return api.GetErrorResponse(err, "DeviceLogin")
}

fmt.Printf("logging into Plural at %s\n", device.LoginUrl)
Expand Down Expand Up @@ -132,7 +132,7 @@ func handleLogin(c *cli.Context) error {
func postLogin(conf *config.Config, client api.Client, c *cli.Context) error {
me, err := client.Me()
if err != nil {
return err
return api.GetErrorResponse(err, "Me")
}

conf.Email = me.Email
Expand All @@ -142,7 +142,7 @@ func postLogin(conf *config.Config, client api.Client, c *cli.Context) error {
if saEmail != "" {
jwt, email, err := client.ImpersonateServiceAccount(saEmail)
if err != nil {
return err
return api.GetErrorResponse(err, "ImpersonateServiceAccount")
}

conf.Email = email
Expand All @@ -153,7 +153,7 @@ func postLogin(conf *config.Config, client api.Client, c *cli.Context) error {

accessToken, err := client.GrabAccessToken()
if err != nil {
return err
return api.GetErrorResponse(err, "GrabAccessToken")
}

conf.Token = accessToken
Expand Down
8 changes: 5 additions & 3 deletions cmd/plural/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"os"

"github.com/pluralsh/plural/pkg/api"

"github.com/olekukonko/tablewriter"
"github.com/pluralsh/plural/pkg/utils"
"github.com/pluralsh/plural/pkg/wkspace"
Expand Down Expand Up @@ -61,15 +63,15 @@ func (p *Plural) uninstallPackage(c *cli.Context) error {
if t == "terraform" {
for _, inst := range space.Terraform {
if inst.Terraform.Name == name {
return p.Client.UninstallTerraform(inst.Id)
return api.GetErrorResponse(p.Client.UninstallTerraform(inst.Id), "UninstallTerraform")
}
}
}

if t == "helm" {
for _, inst := range space.Charts {
if inst.Chart.Name == name {
return p.Client.UninstallChart(inst.Id)
return api.GetErrorResponse(p.Client.UninstallChart(inst.Id), "UninstallChart")
}
}
}
Expand All @@ -82,7 +84,7 @@ func (p *Plural) getWorkspace(repo string) (*wkspace.Workspace, error) {
p.InitPluralClient()
inst, err := p.Client.GetInstallation(repo)
if err != nil {
return nil, err
return nil, api.GetErrorResponse(err, "GetInstallation")
}

return wkspace.New(p.Client, inst)
Expand Down
9 changes: 5 additions & 4 deletions cmd/plural/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func apply(c *cli.Context) error {
func (p *Plural) handleTerraformUpload(c *cli.Context) error {
p.InitPluralClient()
_, err := p.UploadTerraform(c.Args().Get(0), c.Args().Get(1))
return err
return api.GetErrorResponse(err, "UploadTerraform")
}

func handleHelmTemplate(c *cli.Context) error {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (p *Plural) handleRecipeUpload(c *cli.Context) error {
}

_, err = p.CreateRecipe(c.Args().Get(1), recipeInput)
return err
return api.GetErrorResponse(err, "CreateRecipe")
}

func (p *Plural) handleArtifact(c *cli.Context) error {
Expand All @@ -208,13 +208,14 @@ func (p *Plural) handleArtifact(c *cli.Context) error {
input.Platform = c.String("platform")
input.Arch = c.String("arch")
_, err = p.CreateArtifact(c.Args().Get(1), input)
return err
return api.GetErrorResponse(err, "CreateArtifact")
}

func (p *Plural) createCrd(c *cli.Context) error {
p.InitPluralClient()
fullPath, _ := filepath.Abs(c.Args().Get(0))
repo := c.Args().Get(1)
chart := c.Args().Get(2)
return p.CreateCrd(repo, chart, fullPath)
err := p.CreateCrd(repo, chart, fullPath)
return api.GetErrorResponse(err, "CreateCrd")
}
7 changes: 4 additions & 3 deletions cmd/plural/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ func (p *Plural) reposCommands() []cli.Command {

func (p *Plural) handleUnlockRepo(c *cli.Context) error {
p.InitPluralClient()
return p.UnlockRepository(c.Args().First())
err := p.UnlockRepository(c.Args().First())
return api.GetErrorResponse(err, "UnlockRepository")
}

func (p *Plural) handleListRepositories(c *cli.Context) error {
p.InitPluralClient()
repos, err := p.ListRepositories(c.String("query"))
if err != nil {
return err
return api.GetErrorResponse(err, "ListRepositories")
}

addIcon := c.String("format") == "csv"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (p *Plural) handleResetInstallations(c *cli.Context) error {
p.InitPluralClient()
count, err := p.ResetInstallations()
if err != nil {
return err
return api.GetErrorResponse(err, "ResetInstallations")
}

fmt.Printf("Deleted %d installations in app.plural.sh\n", count)
Expand Down
5 changes: 3 additions & 2 deletions cmd/plural/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func handleShellSync(c *cli.Context) error {

shell, err := client.GetShell()
if err != nil {
return err
return api.GetErrorResponse(err, "GetShell")
}

if err := crypto.Setup(shell.AesKey); err != nil {
Expand Down Expand Up @@ -77,5 +77,6 @@ func handleShellPurge(c *cli.Context) error {
}

client := api.NewClient()
return client.DeleteShell()
err := client.DeleteShell()
return api.GetErrorResponse(err, "DeleteShell")
}
2 changes: 1 addition & 1 deletion cmd/plural/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func tracked(fn func(*cli.Context) error, event string) func(*cli.Context) error
if conf.ReportErrors {
client := api.FromConfig(&conf)
if err := client.CreateEvent(&event); err != nil {
return err
return api.GetErrorResponse(err, "CreateEvent")
}
}
return err
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/Azure/go-autorest/autorest/azure/auth v0.5.11
github.com/Azure/go-autorest/autorest/to v0.4.0
github.com/Masterminds/sprig/v3 v3.2.2
github.com/Yamashou/gqlgenc v0.11.0
github.com/azure/azure-sdk-for-go v57.4.0+incompatible
github.com/buger/goterm v1.0.4
github.com/chartmuseum/helm-push v0.10.3
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.0.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.5.1 // indirect
github.com/Yamashou/gqlgenc v0.11.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.16.14 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.3 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.12.18 // indirect
Expand Down Expand Up @@ -210,7 +210,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/pluralsh/oauth v0.9.1-0.20220520000222-d76c0e7a0db9
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
28 changes: 28 additions & 0 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package api

import (
"context"
"encoding/json"
"net/http"

rawclient "github.com/Yamashou/gqlgenc/client"
"github.com/pkg/errors"
"github.com/pluralsh/gqlclient"
"github.com/pluralsh/plural/pkg/config"
)
Expand Down Expand Up @@ -97,3 +100,28 @@ func FromConfig(conf *config.Config) Client {
}

}

func GetErrorResponse(err error, methodName string) error {
if err == nil {
return nil
}
errResponse := &rawclient.ErrorResponse{}
newErr := json.Unmarshal([]byte(err.Error()), errResponse)
if newErr != nil {
return err
}

errList := errors.New(methodName)
if errResponse.GqlErrors != nil {
for _, err := range *errResponse.GqlErrors {
errList = errors.Wrap(errList, err.Message)
}
errList = errors.Wrap(errList, "GraphQL error")
}
if errResponse.NetworkError != nil {
errList = errors.Wrap(errList, errResponse.NetworkError.Message)
errList = errors.Wrap(errList, "Network error")
}

return errList
}
Loading

0 comments on commit 16230fc

Please sign in to comment.