@@ -21,6 +21,7 @@ import (
2121 "context"
2222 _ "embed"
2323 "encoding/json"
24+ goerrors "errors"
2425 "fmt"
2526 "io"
2627 "net/http"
@@ -29,7 +30,7 @@ import (
2930
3031 "github.com/google/uuid"
3132 openapi_types "github.com/oapi-codegen/runtime/types"
32- "github.com/pkg/errors"
33+ errors "github.com/pkg/errors"
3334 "github.com/rs/zerolog"
3435
3536 "github.com/snyk/code-client-go/bundle"
@@ -40,6 +41,7 @@ import (
4041 "github.com/snyk/code-client-go/observability"
4142 "github.com/snyk/code-client-go/sarif"
4243 "github.com/snyk/code-client-go/scan"
44+ "github.com/snyk/error-catalog-golang-public/cli"
4345)
4446
4547//go:generate go tool github.com/golang/mock/mockgen -destination=mocks/analysis.go -source=analysis.go -package mocks
@@ -352,6 +354,9 @@ func (a *analysisOrchestrator) retrieveTestURL(ctx context.Context, client *test
352354 }
353355
354356 return components , true , nil
357+ case string (testModels .Error ):
358+ testError := parseTestError (parsedResponse , method )
359+ return nil , false , testError
355360 default :
356361 return nil , false , fmt .Errorf ("unexpected test status \" %s\" " , stateDiscriminator )
357362 }
@@ -360,6 +365,40 @@ func (a *analysisOrchestrator) retrieveTestURL(ctx context.Context, client *test
360365 }
361366}
362367
368+ func parseTestError (parsedResponse * testApi.GetTestResultResponse , method string ) error {
369+ errorResponse , stateErrorStateError := parsedResponse .ApplicationvndApiJSON200 .Data .Attributes .AsTestErrorState ()
370+
371+ if stateErrorStateError != nil {
372+ return stateErrorStateError
373+ }
374+
375+ if errorResponse .Errors == nil {
376+ return fmt .Errorf ("%s: test error state has no errors" , method )
377+ }
378+
379+ var testError error
380+ for _ , error := range * errorResponse .Errors {
381+ // since the error is only partially defined, we to create an existing generic error and fill it with the available information
382+ tmp := cli .NewGeneralCLIFailureError (error .Message )
383+ tmp .Level = "error"
384+ tmp .ErrorCode = error .ErrorCode
385+ tmp .Title = error .Title
386+ tmp .StatusCode = parsedResponse .StatusCode ()
387+ tmp .Classification = error .Classification
388+
389+ if error .InfoUrl != nil {
390+ tmp .Type = * error .InfoUrl
391+ tmp .Links = []string {}
392+ }
393+ testError = goerrors .Join (testError , tmp )
394+ }
395+
396+ if testError == nil {
397+ testError = fmt .Errorf ("%s: test error state has no errors" , method )
398+ }
399+ return testError
400+ }
401+
363402func (a * analysisOrchestrator ) retrieveTestComponents (ctx context.Context , client * testApi.Client , org uuid.UUID , testId openapi_types.UUID ) (* scan.ResultMetaData , error ) {
364403 method := "analysis.retrieveTestComponents"
365404 logger := a .logger .With ().Str ("method" , method ).Logger ()
0 commit comments