From 398b7d74743fc9924535eed58680b0e034e8ce15 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Fri, 21 Jul 2023 15:02:53 +0100 Subject: [PATCH] fix(kv_store_entry): improve error handling for batch processing (#980) * fix(kv_store_entry): improve error handling for batch processing * fix(deploy): increase timeout size to 10s * build(ci): support controlling the tests that are run --- .github/workflows/pr_test.yml | 2 ++ go.mod | 2 +- go.sum | 4 ++-- pkg/commands/compute/deploy_test.go | 2 +- pkg/commands/kvstoreentry/create.go | 25 +++++++++++++++++++++++-- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pr_test.yml b/.github/workflows/pr_test.yml index 3c79807f6..7c6ece293 100644 --- a/.github/workflows/pr_test.yml +++ b/.github/workflows/pr_test.yml @@ -151,6 +151,8 @@ jobs: run: make test shell: bash env: + # NOTE: The following lets us focus the test run while debugging. + # TEST_ARGS: "-run TestDeploy ./pkg/commands/compute/..." TEST_COMPUTE_INIT: true TEST_COMPUTE_BUILD: true TEST_COMPUTE_DEPLOY: true diff --git a/go.mod b/go.mod index 41a63b912..d753db59b 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( ) require ( - github.com/fastly/go-fastly/v8 v8.5.4 + github.com/fastly/go-fastly/v8 v8.5.5 github.com/kennygrant/sanitize v1.2.4 github.com/mholt/archiver v3.1.1+incompatible github.com/otiai10/copy v1.12.0 diff --git a/go.sum b/go.sum index 185115980..480ad28b3 100644 --- a/go.sum +++ b/go.sum @@ -19,8 +19,8 @@ github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj6 github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0 h1:90Ly+6UfUypEF6vvvW5rQIv9opIL8CbmW9FT20LDQoY= github.com/dustinkirkland/golang-petname v0.0.0-20191129215211-8e5a1ed0cff0/go.mod h1:V+Qd57rJe8gd4eiGzZyg4h54VLHmYVVw54iMnlAMrF8= -github.com/fastly/go-fastly/v8 v8.5.4 h1:uyu5xfnGB0HBELC+TEpCrX01QDP5NbdAbifh1R5duT8= -github.com/fastly/go-fastly/v8 v8.5.4/go.mod h1:m/QWKyZsuH8Ow3tDkRLqlbvbJdxalRGkTT3ZKxZJ4eo= +github.com/fastly/go-fastly/v8 v8.5.5 h1:okmeQDxjyK9FEh5mj2K+AwN9SiZDz3epQHm4dAh3+2s= +github.com/fastly/go-fastly/v8 v8.5.5/go.mod h1:jmjaUGq1RUdP05XOuD1ICvuuzo0EdCexShviy2sFfHU= github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible h1:FhrXlfhgGCS+uc6YwyiFUt04alnjpoX7vgDKJxS6Qbk= github.com/fastly/kingpin v2.1.12-0.20191105091915-95d230a53780+incompatible/go.mod h1:U8UynVoU1SQaqD2I4ZqgYd5lx3A1ipQYn4aSt2Y5h6c= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= diff --git a/pkg/commands/compute/deploy_test.go b/pkg/commands/compute/deploy_test.go index a218ac54c..2e34b3d10 100644 --- a/pkg/commands/compute/deploy_test.go +++ b/pkg/commands/compute/deploy_test.go @@ -1889,7 +1889,7 @@ func TestDeploy(t *testing.T) { select { case <-done: // Wait for app.Run() to finish - case <-time.After(5 * time.Second): + case <-time.After(10 * time.Second): t.Fatalf("unexpected timeout waiting for mocked prompt inputs to be processed") } } else { diff --git a/pkg/commands/kvstoreentry/create.go b/pkg/commands/kvstoreentry/create.go index 1e3c25305..220f33024 100644 --- a/pkg/commands/kvstoreentry/create.go +++ b/pkg/commands/kvstoreentry/create.go @@ -362,7 +362,8 @@ func (c *CreateCommand) PromptWindowsUser(in io.Reader, out io.Writer) (bool, er // CallBatchEndpoint calls the batch API endpoint. func (c *CreateCommand) CallBatchEndpoint(in io.Reader, out io.Writer) error { type result struct { - Success bool `json:"success"` + Success bool `json:"success"` + Errors []*fastly.ErrorObject `json:"errors,omitempty"` } if err := c.Globals.APIClient.BatchModifyKVStoreKey(&fastly.BatchModifyKVStoreKeyInput{ @@ -371,11 +372,31 @@ func (c *CreateCommand) CallBatchEndpoint(in io.Reader, out io.Writer) error { }); err != nil { c.Globals.ErrLog.Add(err) + r := result{Success: false} + + he, ok := err.(*fastly.HTTPError) + if ok { + r.Errors = append(r.Errors, he.Errors...) + } + if c.JSONOutput.Enabled { - _, err := c.WriteJSON(out, result{Success: false}) + _, err := c.WriteJSON(out, r) return err } + // If we were able to convert the error into a fastly.HTTPError, then + // display those errors to the user, otherwise we'll display the original + // error type. + if ok { + for i, e := range he.Errors { + text.Output(out, "Error %d", i) + text.Output(out, "Title: %s", e.Title) + text.Output(out, "Code: %s", e.Code) + text.Output(out, "Detail: %s", e.Detail) + text.Break(out) + } + return he + } return err }