Skip to content

Commit

Permalink
fix(CLI): print msg or errors wrapping exec.ExitError
Browse files Browse the repository at this point in the history
  • Loading branch information
mcombuechen committed Apr 26, 2024
1 parent 1a56a0b commit 60c5edb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 1 addition & 2 deletions cliv2/cmd/cliv2/main.go
Expand Up @@ -370,8 +370,7 @@ func handleError(err error) HandleError {

func displayError(err error, output io.Writer, config configuration.Configuration) {
if err != nil {
var exitError *exec.ExitError
isExitError := errors.As(err, &exitError)
_, isExitError := err.(*exec.ExitError)
isErrorWithCode := errors.As(err, &cli_errors.ErrorWithExitCode{})
if isExitError || isErrorWithCode {
return
Expand Down
14 changes: 14 additions & 0 deletions cliv2/cmd/cliv2/main_test.go
Expand Up @@ -393,4 +393,18 @@ func Test_displayError(t *testing.T) {
assert.Equal(t, "", b.String())
})
}

t.Run("prints messages of error wrapping exec.ExitError", func(t *testing.T) {
var b bytes.Buffer
config := configuration.NewInMemory()
err := &wrErr{wraps: &exec.ExitError{}}
displayError(err, &b, config)

assert.Equal(t, "something went wrong\n", b.String())
})
}

type wrErr struct{ wraps error }

func (e *wrErr) Error() string { return "something went wrong" }
func (e *wrErr) Unwrap() error { return e.wraps }
3 changes: 2 additions & 1 deletion test/jest/acceptance/snyk-sbom/sbom.spec.ts
Expand Up @@ -177,7 +177,7 @@ describe('snyk sbom (mocked server only)', () => {
test('`sbom` retains the exit error code of the underlying SCA process', async () => {
const project = await createProject('empty');

const { code } = await runSnykCLI(
const { code, stdout } = await runSnykCLI(
`sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.5+json --debug`,
{
cwd: project.path(),
Expand All @@ -186,5 +186,6 @@ describe('snyk sbom (mocked server only)', () => {
);

expect(code).toBe(3);
expect(stdout).toContain("An error occurred while running the underlying analysis needed to generate the SBOM.")
});
});

0 comments on commit 60c5edb

Please sign in to comment.