Skip to content

Commit

Permalink
fix: update ReferrerNotFound error to be more accurate (#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li authored Apr 19, 2024
1 parent 56be5bb commit f063058
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
10 changes: 5 additions & 5 deletions errors/pluginerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ var (
Description: `The manifest is invalid. Please validate the manifest is correctly formatted.`,
})

// ErrorCodeReferrersNotFound is returned if there is no ReferrerStore set.
ErrorCodeReferrersNotFound = Register("errcode", ErrorDescriptor{
Value: "REFERRERS_NOT_FOUND",
Message: "referrers not found",
Description: "No referrers are found. Please verify the subject has attached expected artifacts and refer to https://ratify.dev/docs/reference/store/ to investigate Referrer Store configuration.",
// ErrorCodeNoVerifierReport is returned if there is no ReferrerStore set.
ErrorCodeNoVerifierReport = Register("errcode", ErrorDescriptor{
Value: "NO_VERIFIER_REPORT",
Message: "no verifier report",
Description: "No verifier report was generated. This might be due to various factors, such as lack of artifacts attached to the image, a misconfiguration in the Referrer Store preventing access to the registry, or the absence of appropriate verifiers corresponding to the referenced image artifacts.",
})

// Generic errors happen in plugins
Expand Down
19 changes: 19 additions & 0 deletions errors/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Error struct {
PluginName string `json:"pluginName,omitempty"`
LinkToDoc string `json:"linkToDoc,omitempty"`
Stack string `json:"stack,omitempty"`
Description string `json:"description,omitempty"`
}

// ErrorDescriptor provides relevant information about a given error code.
Expand Down Expand Up @@ -124,6 +125,11 @@ func (ec ErrorCode) Message() string {
return ec.Descriptor().Message
}

// Description returned the description of this error code.
func (ec ErrorCode) Description() string {
return ec.Descriptor().Description
}

// String returns the canonical identifier for this error code.
func (ec ErrorCode) String() string {
return ec.Descriptor().Value
Expand All @@ -149,6 +155,10 @@ func (ec ErrorCode) WithLinkToDoc(link string) Error {
return newError(ec, ec.Message()).WithLinkToDoc(link)
}

func (ec ErrorCode) WithDescription() Error {
return newError(ec, ec.Message()).WithDescription()
}

// WithPluginName returns a new Error object with pluginName set.
func (ec ErrorCode) WithPluginName(pluginName string) Error {
return newError(ec, ec.Message()).WithPluginName(pluginName)
Expand Down Expand Up @@ -223,6 +233,10 @@ func (e Error) Error() string {
errStr += fmt.Sprintf(", Detail: %v", e.Detail)
}

if e.Description != "" {
errStr += fmt.Sprintf(", Description: %v", e.Description)
}

if e.Stack != "" {
errStr += fmt.Sprintf(", Stack trace: %s", e.Stack)
}
Expand Down Expand Up @@ -261,6 +275,11 @@ func (e Error) WithLinkToDoc(link string) Error {
return e
}

func (e Error) WithDescription() Error {
e.Description = e.Code.Description()
return e
}

// IsEmpty returns true if the error is empty.
func (e Error) IsEmpty() bool {
return e == emptyError
Expand Down
9 changes: 8 additions & 1 deletion errors/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func TestWithPluginName(t *testing.T) {
}
}

func TestWithDescription(t *testing.T) {
err := testEC.WithDescription()
if err.Description != testDescription {
t.Fatalf("expected description: %s, got: %s", testDescription, err.Description)
}
}

func TestIs(t *testing.T) {
err := testEC.WithDetail(testDetail)
result := err.Is(err)
Expand Down Expand Up @@ -161,7 +168,7 @@ func TestIsEmpty(t *testing.T) {
}

func TestError_Error(t *testing.T) {
err := testEC.WithPluginName(testPluginName).WithComponentType(testComponentType).WithLinkToDoc(testLink).WithDetail(testDetail).WithError(Error{})
err := testEC.WithPluginName(testPluginName).WithComponentType(testComponentType).WithLinkToDoc(testLink).WithDetail(testDetail).WithError(Error{}).WithDescription()
result := err.Error()
if !strings.HasPrefix(result, testErrorString) {
t.Fatalf("expected string starts with: %s, but got: %s", testErrorString, result)
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/core/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (executor Executor) verifySubjectInternal(ctx context.Context, verifyParame
}
if executor.PolicyEnforcer.GetPolicyType(ctx) == pt.ConfigPolicy {
if len(verifierReports) == 0 {
return types.VerifyResult{}, errors.ErrorCodeReferrersNotFound.WithComponentType(errors.Executor)
return types.VerifyResult{}, errors.ErrorCodeNoVerifierReport.WithComponentType(errors.Executor).WithDescription()
}
}
// If it requires embedded Rego Policy Engine make the decision, execute
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/core/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestVerifySubjectInternal_ResolveSubjectDescriptor_Success(t *testing.T) {
Subject: "localhost:5000/net-monitor:v1",
}

if _, err := executor.verifySubjectInternal(context.Background(), verifyParameters); !errors.Is(err, ratifyerrors.ErrorCodeReferrersNotFound.WithDetail("")) {
if _, err := executor.verifySubjectInternal(context.Background(), verifyParameters); !errors.Is(err, ratifyerrors.ErrorCodeNoVerifierReport.WithDetail("")) {
t.Fatalf("expected ErrReferrersNotFound actual %v", err)
}
}
Expand All @@ -214,7 +214,7 @@ func TestVerifySubjectInternal_Verify_NoReferrers(t *testing.T) {
Subject: "localhost:5000/net-monitor:v1",
}

if _, err := ex.verifySubjectInternal(context.Background(), verifyParameters); !errors.Is(err, ratifyerrors.ErrorCodeReferrersNotFound.WithDetail("")) {
if _, err := ex.verifySubjectInternal(context.Background(), verifyParameters); !errors.Is(err, ratifyerrors.ErrorCodeNoVerifierReport.WithDetail("")) {
t.Fatalf("expected ErrReferrersNotFound actual %v", err)
}
}
Expand Down

0 comments on commit f063058

Please sign in to comment.