Skip to content

Commit

Permalink
fix: display error logic to handle ExitCode errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer authored and thisislawatts committed Apr 15, 2024
1 parent e09cb58 commit 340cdac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
36 changes: 20 additions & 16 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

// !!! This import needs to be the first import, please do not change this !!!
import (
"os/exec"

_ "github.com/snyk/go-application-framework/pkg/networking/fips_enable"
)

Expand All @@ -12,7 +14,6 @@ import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -373,23 +374,26 @@ func handleError(err error) HandleError {
func displayError(err error, output io.Writer, config configuration.Configuration) {
if err != nil {
var exitError *exec.ExitError
if !errors.As(err, &exitError) {
// Test 3
if config.GetBool(localworkflows.OUTPUT_CONFIG_KEY_JSON) {
jsonError := JsonErrorStruct{
Ok: false,
ErrorMsg: err.Error(),
Path: globalConfiguration.GetString(configuration.INPUT_DIRECTORY),
}
isExitError := errors.As(err, &exitError)
isErrorWithCode := errors.As(err, &cli_errors.ErrorWithExitCode{})
if isExitError || isErrorWithCode {
return
}

if config.GetBool(localworkflows.OUTPUT_CONFIG_KEY_JSON) {
jsonError := JsonErrorStruct{
Ok: false,
ErrorMsg: err.Error(),
Path: globalConfiguration.GetString(configuration.INPUT_DIRECTORY),
}

jsonErrorBuffer, _ := json.MarshalIndent(jsonError, "", " ")
fmt.Fprintln(output, string(jsonErrorBuffer))
jsonErrorBuffer, _ := json.MarshalIndent(jsonError, "", " ")
fmt.Fprintln(output, string(jsonErrorBuffer))
} else {
if errors.Is(err, context.DeadlineExceeded) {
fmt.Fprintln(output, "command timed out")
} else {
if errors.Is(err, context.DeadlineExceeded) {
fmt.Fprintln(output, "command timed out")
} else {
fmt.Fprintln(output, err)
}
fmt.Fprintln(output, err)
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions cliv2/cmd/cliv2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"time"

"github.com/rs/zerolog"
"github.com/snyk/cli/cliv2/internal/constants"
clierrors "github.com/snyk/cli/cliv2/internal/errors"
"github.com/snyk/go-application-framework/pkg/configuration"
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
Expand All @@ -22,6 +20,9 @@ import (
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/snyk/cli/cliv2/internal/constants"
clierrors "github.com/snyk/cli/cliv2/internal/errors"
)

func cleanup() {
Expand Down Expand Up @@ -380,8 +381,8 @@ func Test_displayError(t *testing.T) {
err error
}{
{
name: "exec.Error",
err: &exec.Error{},
name: "exec.ExitError",
err: &exec.ExitError{},
},
{
name: "clierrors.ErrorWithExitCode",
Expand All @@ -396,7 +397,7 @@ func Test_displayError(t *testing.T) {
err := scenario.err
displayError(err, &b, config)

assert.Contains(t, b.String(), "")
assert.Equal(t, "", b.String())
})
}
}

0 comments on commit 340cdac

Please sign in to comment.