Skip to content

Commit

Permalink
fix(code): Fix error handling for experimental go native code client (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer authored Apr 16, 2024
1 parent 86484c9 commit 5400c69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func getErrorFromWorkFlowData(data []workflow.Data) error {
// this should be supported in the future
for _, result := range summary.Results {
if result.Open > 0 {
return cli_errors.ErrorWithExitCode{
return &cli_errors.ErrorWithExitCode{
ExitCode: constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND,
}
}
Expand Down Expand Up @@ -371,8 +371,9 @@ func handleError(err error) HandleError {
func displayError(err error, output io.Writer, config configuration.Configuration) {
if err != nil {
var exitError *exec.ExitError
var exitCode *cli_errors.ErrorWithExitCode
isExitError := errors.As(err, &exitError)
isErrorWithCode := errors.As(err, &cli_errors.ErrorWithExitCode{})
isErrorWithCode := errors.As(err, &exitCode)
if isExitError || isErrorWithCode {
return
}
Expand Down
17 changes: 12 additions & 5 deletions cliv2/cmd/cliv2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"
clierrors "github.com/snyk/cli/cliv2/internal/errors"
)
Expand Down Expand Up @@ -253,7 +254,9 @@ func Test_getErrorFromWorkFlowData(t *testing.T) {
data := workflow.NewData(workflowIdentifier, content_type.TEST_SUMMARY, payload)
err = getErrorFromWorkFlowData([]workflow.Data{data})
require.NotNil(t, err)
assert.ErrorIs(t, err, clierrors.ErrorWithExitCode{ExitCode: constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND})
var expectedError *clierrors.ErrorWithExitCode
assert.ErrorAs(t, err, &expectedError)
assert.Equal(t, constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND, expectedError.ExitCode)
})

t.Run("workflow with empty testing findings", func(t *testing.T) {
Expand Down Expand Up @@ -338,9 +341,13 @@ func Test_runWorkflowAndProcessData(t *testing.T) {
// invoke method under test
logger := zerolog.New(os.Stderr)
err = runWorkflowAndProcessData(globalEngine, &logger, testCmnd)
assert.ErrorIs(t, err, clierrors.ErrorWithExitCode{
ExitCode: constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND,
})

var expectedError *clierrors.ErrorWithExitCode
assert.ErrorAs(t, err, &expectedError)
assert.Equal(t, constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND, expectedError.ExitCode)

actualCode := cliv2.DeriveExitCode(err)
assert.Equal(t, constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND, actualCode)
}

func Test_setTimeout(t *testing.T) {
Expand Down Expand Up @@ -379,7 +386,7 @@ func Test_displayError(t *testing.T) {
},
{
name: "clierrors.ErrorWithExitCode",
err: clierrors.ErrorWithExitCode{ExitCode: 42},
err: &clierrors.ErrorWithExitCode{ExitCode: 42},
},
}

Expand Down
3 changes: 2 additions & 1 deletion cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
"time"

"github.com/gofrs/flock"
cli_errors "github.com/snyk/cli/cliv2/internal/errors"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/utils"

cli_errors "github.com/snyk/cli/cliv2/internal/errors"

"github.com/snyk/cli/cliv2/internal/constants"
"github.com/snyk/cli/cliv2/internal/embedded"
"github.com/snyk/cli/cliv2/internal/embedded/cliv1"
Expand Down

0 comments on commit 5400c69

Please sign in to comment.