Skip to content

Commit

Permalink
chore: add error catalog errors to the end of logs on CLI failure
Browse files Browse the repository at this point in the history
  • Loading branch information
j-luong committed Jan 24, 2025
1 parent f07f78c commit 73d0c26
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import _ "github.com/snyk/go-application-framework/pkg/networking/fips_enable"
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/snyk/go-application-framework/pkg/local_workflows/config_utils"

"github.com/snyk/error-catalog-golang-public/snyk_errors"
"github.com/snyk/go-application-framework/pkg/auth"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/local_workflows/config_utils"

"github.com/snyk/cli/cliv2/internal/cliv2"
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/networking"
"github.com/snyk/go-application-framework/pkg/networking/fips"

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

func logHeaderAuthorizationInfo(
Expand Down Expand Up @@ -82,6 +84,14 @@ func getFipsStatus(config configuration.Configuration) string {
return fipsEnabled
}

func tablePrint(name string, value string) {
title := name
if len(name) > 0 {
title = title + ":"
}
globalLogger.Printf("%-22s %s", title, value)
}

func writeLogHeader(config configuration.Configuration, networkAccess networking.NetworkAccess) {
authorization, _, userAgent := logHeaderAuthorizationInfo(config, networkAccess)

Expand All @@ -101,10 +111,6 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking
previewFeaturesEnabled = "enabled"
}

tablePrint := func(name string, value string) {
globalLogger.Printf("%-22s %s", name+":", value)
}

fipsEnabled := getFipsStatus(config)

tablePrint("Version", cliv2.GetFullVersion()+" "+buildType)
Expand Down Expand Up @@ -136,3 +142,30 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking
tablePrint(" Configuration", "all good")
}
}

func writeLogFooter(exitCode int, errs []error) {
tablePrint("Exit Code", strconv.Itoa(exitCode))

// output error details
if exitCode > 1 && len(errs) > 0 {
for i, err := range errs {
var snykError snyk_errors.Error
if errors.As(err, &snykError) {
tablePrint(fmt.Sprintf("Error (%d)", i+1), snykError.ErrorCode)
tablePrint("", snykError.Title)
tablePrint("", snykError.Classification)
tablePrint("", snykError.Description)
tablePrint("", snykError.Type)

if len(snykError.Detail) > 0 {
tablePrint("", snykError.Detail)
}
if len(snykError.Meta) > 0 {
for k, v := range snykError.Meta {
tablePrint("", fmt.Sprintf("%s: %s", k, v.(string)))
}
}
}
}
}
}
16 changes: 8 additions & 8 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ const (
)

func main() {
errorCode := MainWithErrorCode()
globalLogger.Printf("Exiting with %d", errorCode)
errorCode, errs := MainWithErrorCode()
writeLogFooter(errorCode, errs)
os.Exit(errorCode)
}

Expand Down Expand Up @@ -478,9 +478,12 @@ func displayError(err error, userInterface ui.UserInterface, config configuratio
}
}

func MainWithErrorCode() int {
func MainWithErrorCode() (int, []error) {
initDebugBuild()

errorList := []error{}
errorListMutex := sync.Mutex{}

startTime := time.Now()
var err error
rInfo := runtimeinfo.New(runtimeinfo.WithName("snyk-cli"), runtimeinfo.WithVersion(cliv2.GetFullVersion()))
Expand Down Expand Up @@ -528,7 +531,7 @@ func MainWithErrorCode() int {
err = globalEngine.Init()
if err != nil {
globalLogger.Print("Failed to init Workflow Engine!", err)
return constants.SNYK_EXIT_CODE_ERROR
return constants.SNYK_EXIT_CODE_ERROR, errorList
}

// add output flags as persistent flags
Expand All @@ -539,9 +542,6 @@ func MainWithErrorCode() int {
// add workflows as commands
createCommandsForWorkflows(rootCommand, globalEngine)

errorList := []error{}
errorListMutex := sync.Mutex{}

// init NetworkAccess
ua := networking.UserAgent(networking.UaWithConfig(globalConfiguration), networking.UaWithRuntimeInfo(rInfo), networking.UaWithOS(internalOS))
networkAccess := globalEngine.GetNetworkAccess()
Expand Down Expand Up @@ -633,7 +633,7 @@ func MainWithErrorCode() int {
globalLogger.Printf("Failed to cleanup %v", err)
}

return exitCode
return exitCode, errorList
}

func legacyCLITerminated(err error, errorList []error) error {
Expand Down

0 comments on commit 73d0c26

Please sign in to comment.