Skip to content

Commit

Permalink
chore: refactor to append interaction id to error output
Browse files Browse the repository at this point in the history
  • Loading branch information
thisislawatts committed Feb 21, 2025
1 parent cdf4b3a commit 008d6ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 65 deletions.
37 changes: 8 additions & 29 deletions cliv2/cmd/cliv2/errorhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
24 changes: 6 additions & 18 deletions cliv2/cmd/cliv2/errorhandling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,56 +14,45 @@ 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) {
err1 := cli.NewConnectionTimeoutError("")
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)
Expand Down
7 changes: 2 additions & 5 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 13 additions & 13 deletions test/jest/acceptance/error-catalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down

0 comments on commit 008d6ad

Please sign in to comment.