Skip to content

Commit

Permalink
chore: include interaction id on failed run
Browse files Browse the repository at this point in the history
  • Loading branch information
sandor-trombitas committed Feb 13, 2025
1 parent c0efca4 commit 3e0391d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
39 changes: 30 additions & 9 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"github.com/snyk/cli-extension-dep-graph/pkg/depgraph"
"github.com/snyk/cli-extension-iac-rules/iacrules"
"github.com/snyk/cli-extension-sbom/pkg/sbom"
"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"
"github.com/snyk/container-cli/pkg/container"
"github.com/snyk/error-catalog-golang-public/snyk_errors"
"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/app"
"github.com/snyk/go-application-framework/pkg/configuration"
Expand All @@ -30,9 +33,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"

localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
"github.com/snyk/go-application-framework/pkg/local_workflows/json_schemas"
Expand Down Expand Up @@ -453,7 +453,22 @@ func handleError(err error) HandleError {
return resultError
}

func displayError(err error, userInterface ui.UserInterface, config configuration.Configuration) {
func injectInteractionId(err error, interactionId string) error {
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("command timed out")
}
var snykError snyk_errors.Error
if errors.As(err, &snykError) {
if snykError.Meta == nil {
snykError.Meta = make(map[string]interface{})
}
snykError.Meta["interactionId"] = interactionId
err = error(snykError)
}
return err
}

func displayError(err error, userInterface ui.UserInterface, config configuration.Configuration, interactionId string) {
if err != nil {
_, isExitError := err.(*exec.ExitError)
_, isErrorWithCode := err.(*cli_errors.ErrorWithExitCode)
Expand All @@ -471,10 +486,7 @@ func displayError(err error, userInterface ui.UserInterface, config configuratio
jsonErrorBuffer, _ := json.MarshalIndent(jsonError, "", " ")
userInterface.Output(string(jsonErrorBuffer))
} else {
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("command timed out")
}

err = injectInteractionId(err, interactionId)
uiError := userInterface.OutputError(err)
if uiError != nil {
globalLogger.Err(uiError).Msg("ui failed to show error")
Expand Down Expand Up @@ -607,11 +619,13 @@ func MainWithErrorCode() (int, []error) {
err = legacyCLITerminated(err, errorList)
}

displayError(err, globalEngine.GetUserInterface(), globalConfiguration)
displayError(err, globalEngine.GetUserInterface(), globalConfiguration, interactionId)

exitCode := cliv2.DeriveExitCode(err)
globalLogger.Printf("Deriving Exit Code %d (cause: %v)", exitCode, err)

displayInteractionId(exitCode, globalEngine.GetUserInterface(), interactionId)

targetId, targetIdError := instrumentation.GetTargetId(globalConfiguration.GetString(configuration.INPUT_DIRECTORY), instrumentation.AutoDetectedTargetId, instrumentation.WithConfiguredRepository(globalConfiguration))
if targetIdError != nil {
globalLogger.Printf("Failed to derive target id, %v", targetIdError)
Expand Down Expand Up @@ -664,3 +678,10 @@ func setTimeout(config configuration.Configuration, onTimeout func()) {
onTimeout()
}()
}

func displayInteractionId(exitCode int, ui ui.UserInterface, interactionId string) {
if exitCode > 1 {
ui.Output("\n")
ui.Output(fmt.Sprintf("Interaction ID: %s", interactionId))
}
}
2 changes: 1 addition & 1 deletion cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/snyk/code-client-go v1.13.0 // indirect
github.com/snyk/code-client-go v1.13.1 // indirect
github.com/snyk/policy-engine v0.33.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/sourcegraph/go-lsp v0.0.0-20240223163137-f80c5dd31dfd // indirect
Expand Down
6 changes: 2 additions & 4 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,12 @@ github.com/snyk/cli-extension-iac-rules v0.0.0-20250121103856-ea5f31e53509 h1:4L
github.com/snyk/cli-extension-iac-rules v0.0.0-20250121103856-ea5f31e53509/go.mod h1:eaSq1kwY3uGF8bGVYDuv2E3AbzOlCRhdseGhKPvG4OM=
github.com/snyk/cli-extension-sbom v0.0.0-20241016065306-0df2be5b3b8f h1:dlL+f+5sjHj4JCzW/Evl1x9UREXLyc3M4KjoZvQx0Bs=
github.com/snyk/cli-extension-sbom v0.0.0-20241016065306-0df2be5b3b8f/go.mod h1:5CaY1bgvJY/uoG/1plLOf8T8o9AkwoBIGvw34RfRLZw=
github.com/snyk/code-client-go v1.13.0 h1:Hjlfdie7DbMvIDaDdCuY29Z9Z4isvFuAX+u3wevCjWA=
github.com/snyk/code-client-go v1.13.0/go.mod h1:jMRi+hf0T4472k390nGIM5/ZA7bufXlD72TBPmuWdVQ=
github.com/snyk/code-client-go v1.13.1 h1:JWQIrHOg/HYs0yqLmv1kNASwNhKXSYOcZsHU3QEugLk=
github.com/snyk/code-client-go v1.13.1/go.mod h1:jMRi+hf0T4472k390nGIM5/ZA7bufXlD72TBPmuWdVQ=
github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7 h1:Zn5BcV76oFAbJm5tDygU945lvoZ3yY8FoRFDC3YpwF8=
github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM=
github.com/snyk/error-catalog-golang-public v0.0.0-20250121101159-e6a61b2bfae6 h1:qY954YMn/7TaapgatD1bn4hfGQSmu56W6EgS2m8c++I=
github.com/snyk/error-catalog-golang-public v0.0.0-20250121101159-e6a61b2bfae6/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20250210203133-5b0f3252d77d h1:Qhe0N1em8naODCCTXU12UHC4Te9FgwYAS32pzyRIAi8=
github.com/snyk/go-application-framework v0.0.0-20250210203133-5b0f3252d77d/go.mod h1:ag3sCoHGCn534oDAnAXLcjZEZljW3oMOfKCzpmFX060=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/policy-engine v0.33.0 h1:nXH4LEVrYbEuSEq4RJBObRY2fduaXiovAJt3Kni1baY=
Expand Down

0 comments on commit 3e0391d

Please sign in to comment.