Skip to content

Commit

Permalink
fix(sbom): Retain error code during SBOM generation (#5202)
Browse files Browse the repository at this point in the history
* fix(sbom): Retain error code during SBOM generation

* fix(cli): Print message of errors wrapping exec.ExitError
  • Loading branch information
mcombuechen authored Apr 30, 2024
1 parent cf16470 commit 5e98aaa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
3 changes: 1 addition & 2 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ 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)
_, isExitError := err.(*exec.ExitError)
isErrorWithCode := errors.As(err, &exitCode)
if isExitError || isErrorWithCode {
return
Expand Down
14 changes: 14 additions & 0 deletions cliv2/cmd/cliv2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,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 }
4 changes: 2 additions & 2 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (
github.com/google/uuid v1.6.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.32.0
github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73
github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e
github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672
github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413
github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1
github.com/snyk/go-application-framework v0.0.0-20240426112848-21dc9ca24101
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65
Expand Down
8 changes: 4 additions & 4 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A=
github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo=
github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73 h1:rwdJzpPWkWWf7csbv871GoKgRGwJ4GVNXMSsO0cRuLk=
github.com/snyk/cli-extension-dep-graph v0.0.0-20230926124856-b0fdf1ee6f73/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU=
github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e h1:j17Ujw51/2SC3m1hbNCUwxFc8aNIFyfpnwFAszgEM8c=
github.com/snyk/cli-extension-dep-graph v0.0.0-20240426125928-8d56ac52821e/go.mod h1:QF3v8HBpOpyudYNCuR8LqfULutO76c91sBdLzD+pBJU=
github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 h1:AkLej8Lk//vFex1fiygSYFrQTUd0xP+GyRbsI+m2kwQ=
github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672/go.mod h1:2vKTUsW73sVbDcyD19iNLfN0so2GSu9BE3k/fqG0mjA=
github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413 h1:DrieS0CPi1GEnAv8xdUJ7ceqgqUaFJyJAXIZ4I24KCI=
github.com/snyk/cli-extension-sbom v0.0.0-20240426072511-94652d584413/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g=
github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 h1:nLbplrdYB4ljdFSnpmw2rCqmBVxHcvZOFEqc8JF9OQc=
github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4/go.mod h1:lqmQT+QdzLdfi7qsqIH4qvCsSWu+P09GDFwQcmFfC0g=
github.com/snyk/code-client-go v1.5.3 h1:EnHogM2uNnvkrfIJbkOGa9X1bOkU6bJR/QRQCwbK9gQ=
github.com/snyk/code-client-go v1.5.3/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ=
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA=
Expand Down
25 changes: 23 additions & 2 deletions test/jest/acceptance/snyk-sbom/sbom.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createProjectFromWorkspace } from '../../util/createProject';
import * as fs from 'fs';

import {
createProject,
createProjectFromWorkspace,
} from '../../util/createProject';
import { runSnykCLI } from '../../util/runSnykCLI';
import { fakeServer } from '../../../acceptance/fake-server';
import * as fs from 'fs';

jest.setTimeout(1000 * 60 * 5);

Expand Down Expand Up @@ -169,4 +173,21 @@ 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, stdout } = await runSnykCLI(
`sbom --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.5+json --debug`,
{
cwd: project.path(),
env,
},
);

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

0 comments on commit 5e98aaa

Please sign in to comment.