From 008d6ad7573af4a03fd6c67d570cac07d3ebad59 Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Fri, 21 Feb 2025 10:07:05 +0100 Subject: [PATCH] chore: refactor to append interaction id to error output --- cliv2/cmd/cliv2/errorhandling.go | 37 +++++----------------- cliv2/cmd/cliv2/errorhandling_test.go | 24 ++++---------- cliv2/cmd/cliv2/main.go | 7 ++-- test/jest/acceptance/error-catalog.spec.ts | 26 +++++++-------- 4 files changed, 29 insertions(+), 65 deletions(-) diff --git a/cliv2/cmd/cliv2/errorhandling.go b/cliv2/cmd/cliv2/errorhandling.go index 5c69cc8ef1..43a5058e14 100644 --- a/cliv2/cmd/cliv2/errorhandling.go +++ b/cliv2/cmd/cliv2/errorhandling.go @@ -4,13 +4,14 @@ import ( "errors" "os/exec" - cli_errors "github.com/snyk/cli/cliv2/internal/errors" "github.com/snyk/error-catalog-golang-public/cli" "github.com/snyk/error-catalog-golang-public/snyk_errors" + + cli_errors "github.com/snyk/cli/cliv2/internal/errors" ) // decorate generic errors that do not contain Error-Catalog Errors -func decorateError(err error, meta map[string]any) error { +func decorateError(err error) error { if err == nil { return nil } @@ -24,32 +25,10 @@ func decorateError(err error, meta map[string]any) error { } var errorCatalogError snyk_errors.Error - if errors.As(err, &errorCatalogError) { - for k, v := range meta { - snyk_errors.WithMeta(k, v)(&errorCatalogError) - } - - // Rebuild the error chain, replacing the original errorCatalogError - var newErr error = errorCatalogError - current := err - for current != nil { - if current.Error() == errorCatalogError.Error() { - current = errors.Unwrap(current) - continue - } - newErr = errors.Join(newErr, current) - current = errors.Unwrap(current) - } - - return newErr + if !errors.As(err, &errorCatalogError) { + genericError := cli.NewGeneralCLIFailureError(err.Error()) + genericError.StatusCode = 0 + err = errors.Join(err, genericError) } - - genericError := cli.NewGeneralCLIFailureError(err.Error()) - genericError.StatusCode = 0 - - for k, v := range meta { - snyk_errors.WithMeta(k, v)(&genericError) - } - - return errors.Join(err, genericError) + return err } diff --git a/cliv2/cmd/cliv2/errorhandling_test.go b/cliv2/cmd/cliv2/errorhandling_test.go index bde15207e0..13bd00532b 100644 --- a/cliv2/cmd/cliv2/errorhandling_test.go +++ b/cliv2/cmd/cliv2/errorhandling_test.go @@ -6,7 +6,6 @@ import ( "os/exec" "testing" - "github.com/snyk/error-catalog-golang-public/snyk_errors" "github.com/stretchr/testify/assert" "github.com/snyk/error-catalog-golang-public/cli" @@ -15,9 +14,8 @@ import ( ) func Test_decorateError(t *testing.T) { - meta := map[string]any{} t.Run("is nil error", func(t *testing.T) { - assert.Nil(t, decorateError(nil, meta)) + assert.Nil(t, decorateError(nil)) }) t.Run("preserves nested ExitError", func(t *testing.T) { @@ -25,46 +23,36 @@ func Test_decorateError(t *testing.T) { err2 := &exec.ExitError{ ProcessState: &os.ProcessState{}, } - actualErr := decorateError(errors.Join(err1, err2), meta) + actualErr := decorateError(errors.Join(err1, err2)) // Assert that err2 is present in actualErr if !errors.Is(actualErr, err2) { t.Errorf("Expected actualErr to contain err2, but it did not") } }) - t.Run("adds metadata to snyk_error", func(t *testing.T) { - metaValues := map[string]any{"Foo": "bar"} - err := cli.NewConnectionTimeoutError("") - actualErr := decorateError(err, metaValues) - var ecError snyk_errors.Error - if errors.As(actualErr, &ecError) { - assert.Equal(t, metaValues, ecError.Meta) - } - }) - t.Run("is ErrorWithExitCode", func(t *testing.T) { err := &cli_errors.ErrorWithExitCode{ ExitCode: 2, } - assert.Equal(t, err, decorateError(err, meta)) + assert.Equal(t, err, decorateError(err)) }) t.Run("is ExitError", func(t *testing.T) { err := &exec.ExitError{ ProcessState: &os.ProcessState{}, } - assert.Equal(t, err, decorateError(err, meta)) + assert.Equal(t, err, decorateError(err)) }) t.Run("is already error catalog error", func(t *testing.T) { err := cli.NewConnectionTimeoutError("") - actualErr := decorateError(err, meta) + actualErr := decorateError(err) assert.Equal(t, err, actualErr) }) t.Run("is a generic error", func(t *testing.T) { err := errors.New("generic error") - actualErr := decorateError(err, meta) + actualErr := decorateError(err) expectedError := cli.NewGeneralCLIFailureError("") assert.ErrorIs(t, actualErr, err) assert.ErrorAs(t, actualErr, &expectedError) diff --git a/cliv2/cmd/cliv2/main.go b/cliv2/cmd/cliv2/main.go index b772ee7e76..addde4cd55 100644 --- a/cliv2/cmd/cliv2/main.go +++ b/cliv2/cmd/cliv2/main.go @@ -477,6 +477,7 @@ func displayError(err error, userInterface ui.UserInterface, config configuratio if uiError != nil { globalLogger.Err(uiError).Msg("ui failed to show error") } + userInterface.Output(fmt.Sprintf("\nID: %s", interactionId)) } } } @@ -595,11 +596,7 @@ func MainWithErrorCode() (int, []error) { } if err != nil { - // add any meta fields to the err - meta := map[string]any{ - "interactionId": interactionId, - } - err = decorateError(err, meta) + err = decorateError(err) errorList = append(errorList, err) for _, tempError := range errorList { diff --git a/test/jest/acceptance/error-catalog.spec.ts b/test/jest/acceptance/error-catalog.spec.ts index b68f1430cf..773b946e79 100644 --- a/test/jest/acceptance/error-catalog.spec.ts +++ b/test/jest/acceptance/error-catalog.spec.ts @@ -13,20 +13,20 @@ interface Workflow { const integrationWorkflows: Workflow[] = [ { type: 'typescript', - cmd: 'test', + cmd: 'test -d', + }, + { + type: 'golang/native', + cmd: 'code test', + }, + { + type: 'typescript', + cmd: 'monitor', + }, + { + type: 'typescript', + cmd: `container monitor ${TEST_DISTROLESS_STATIC_IMAGE}`, }, - // { - // type: 'golang/native', - // cmd: 'code test', - // }, - // { - // type: 'typescript', - // cmd: 'monitor', - // }, - // { - // type: 'typescript', - // cmd: `container monitor ${TEST_DISTROLESS_STATIC_IMAGE}`, - // }, ]; describe.each(integrationWorkflows)(