Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: display a unique interaction id alongside each error #5737

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
14 changes: 10 additions & 4 deletions cliv2/cmd/cliv2/errorhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"errors"
"github.com/snyk/error-catalog-golang-public/cli"
"os/exec"

"github.com/snyk/error-catalog-golang-public/cli"
"github.com/snyk/error-catalog-golang-public/snyk_errors"
"github.com/snyk/go-application-framework/pkg/networking/middleware"

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

// decorate generic errors that do not contain Error-Catalog Errors
func decorateError(err error) error {
func decorateError(err error, meta map[string]any) error {
if err == nil {
return nil
}
Expand All @@ -25,10 +26,15 @@ func decorateError(err error) error {
}

var errorCatalogError snyk_errors.Error
if !errors.As(err, &errorCatalogError) {
var errWithMeta error
if errors.As(err, &errorCatalogError) {
errWithMeta = middleware.AddMetaDataToErr(errorCatalogError, meta)
} else {
genericError := cli.NewGeneralCLIFailureError(err.Error())
genericError.StatusCode = 0
err = errors.Join(err, genericError)
errWithMeta = middleware.AddMetaDataToErr(genericError, meta)
}

err = errors.Join(err, errWithMeta)
return err
}
16 changes: 9 additions & 7 deletions cliv2/cmd/cliv2/errorhandling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,36 @@ import (
)

func Test_decorateError(t *testing.T) {
meta := map[string]any{}
t.Run("is nil error", func(t *testing.T) {
assert.Nil(t, decorateError(nil))
assert.Nil(t, decorateError(nil, meta))
})

t.Run("is ErrorWithExitCode", func(t *testing.T) {
err := &cli_errors.ErrorWithExitCode{
ExitCode: 2,
}
assert.Equal(t, err, decorateError(err))
assert.Equal(t, err, decorateError(err, meta))
})

t.Run("is ExitError", func(t *testing.T) {
err := &exec.ExitError{
ProcessState: &os.ProcessState{},
}
assert.Equal(t, err, decorateError(err))
assert.Equal(t, err, decorateError(err, meta))
})

t.Run("is already error catalog error", func(t *testing.T) {
err := cli.NewConnectionTimeoutError("")
assert.Equal(t, err, decorateError(err))
actualErr := decorateError(err, meta)
assert.ErrorAs(t, actualErr, &err)
})

t.Run("is a generic error", func(t *testing.T) {
err := errors.New("generic error")
actualErrr := decorateError(err)
actualErr := decorateError(err, meta)
expectedError := cli.NewGeneralCLIFailureError("")
assert.ErrorIs(t, actualErrr, err)
assert.ErrorAs(t, actualErrr, &expectedError)
assert.ErrorIs(t, actualErr, err)
assert.ErrorAs(t, actualErr, &expectedError)
})
}
1 change: 1 addition & 0 deletions cliv2/cmd/cliv2/logheaderfooter.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func writeLogFooter(exitCode int, errs []error) {
}
}
}
tablePrint("Interaction", interactionId)
tablePrint("Exit Code", strconv.Itoa(exitCode))
}

Expand Down
17 changes: 8 additions & 9 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/app"
Expand All @@ -30,9 +32,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 All @@ -57,7 +56,7 @@ var helpProvided bool

var noopLogger zerolog.Logger = zerolog.New(io.Discard)
var globalLogger *zerolog.Logger = &noopLogger
var interactionId = uuid.NewString()
var interactionId = instrumentation.AssembleUrnFromUUID(uuid.NewString())

const (
unknownCommandMessage string = "unknown command"
Expand Down Expand Up @@ -471,10 +470,6 @@ 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")
}

uiError := userInterface.OutputError(err)
if uiError != nil {
globalLogger.Err(uiError).Msg("ui failed to show error")
Expand Down Expand Up @@ -597,7 +592,11 @@ func MainWithErrorCode() (int, []error) {
}

if err != nil {
err = decorateError(err)
// add any meta fields to the err
meta := map[string]any{
"interactionId": interactionId,
}
err = decorateError(err, meta)

errorList = append(errorList, err)
for _, tempError := range errorList {
Expand Down
30 changes: 16 additions & 14 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/snyk/cli-extension-sbom v0.0.0-20241016065306-0df2be5b3b8f
github.com/snyk/container-cli v0.0.0-20240821111304-7ca1c415a5d7
github.com/snyk/error-catalog-golang-public v0.0.0-20250121101159-e6a61b2bfae6
github.com/snyk/go-application-framework v0.0.0-20250212121528-2e787c13a2cc
github.com/snyk/go-application-framework v0.0.0-20250219152506-780621a1e630
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65
github.com/snyk/snyk-iac-capture v0.6.5
github.com/snyk/snyk-ls v0.0.0-20250213084108-c87400c00e06
Expand Down Expand Up @@ -65,15 +65,15 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/dprotaso/go-yit v0.0.0-20240618133044-5a0af90af097 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/erikgeiser/promptkit v0.8.0 // indirect
github.com/erni27/imcache v1.2.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gertd/go-pluralize v0.2.1 // indirect
github.com/getkin/kin-openapi v0.128.0 // indirect
github.com/getkin/kin-openapi v0.129.0 // indirect
github.com/getsentry/sentry-go v0.28.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.6.1 // indirect
Expand Down Expand Up @@ -110,7 +110,6 @@ require (
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
Expand All @@ -124,7 +123,7 @@ require (
github.com/klauspost/compress v1.17.9 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -142,6 +141,8 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oapi-codegen/oapi-codegen/v2 v2.4.1 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/oasdiff/yaml v0.0.0-20241214135536-5f7845c759c8 // indirect
github.com/oasdiff/yaml3 v0.0.0-20241214160948-977117996672 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/open-policy-agent/opa v0.69.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand Down Expand Up @@ -173,7 +174,8 @@ require (
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
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
github.com/speakeasy-api/jsonpath v0.6.1 // indirect
github.com/speakeasy-api/openapi-overlay v0.10.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/viper v1.18.2 // indirect
Expand Down Expand Up @@ -203,17 +205,17 @@ require (
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/term v0.29.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.29.0 // indirect
golang.org/x/tools v0.30.0 // indirect
google.golang.org/api v0.195.0 // indirect
google.golang.org/genproto v0.0.0-20240827150818-7e3bb234dfed // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240827150818-7e3bb234dfed // indirect
Expand Down
Loading
Loading