Skip to content

Commit 3af5ed7

Browse files
feat: 737 add credentials error handling (#1201)
#### What this PR does / why we need it Credentials error handling from #1191 #### Which issue(s) this PR fixes Contributes: open-component-model/ocm-project#737 --------- Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
1 parent f32fa57 commit 3af5ed7

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

bindings/go/credentials/graph.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ var ErrNoDirectCredentials = errors.New("no direct credentials found in graph")
2121
// credentials for the given identity.
2222
var ErrNoIndirectCredentials = errors.New("no indirect credentials found in graph")
2323

24+
// ErrNotFound is returned when no credentials could be found for the given identity.
25+
var ErrNotFound = errors.New("credentials not found")
26+
27+
// ErrUnknown is a generic error indicating an unknown failure during credential resolution.
28+
var ErrUnknown = errors.New("unknown error occurred")
29+
2430
var scheme = runtime.NewScheme()
2531

2632
func init() {
@@ -73,6 +79,7 @@ type Graph struct {
7379
// falls back to indirect resolution through plugins.
7480
func (g *Graph) Resolve(ctx context.Context, identity runtime.Identity) (map[string]string, error) {
7581
if _, err := identity.ParseType(); err != nil {
82+
err = errors.Join(ErrUnknown, err)
7683
return nil, fmt.Errorf("to be resolved from the credential graph, a consumer identity type is required: %w", err)
7784
}
7885

@@ -86,6 +93,13 @@ func (g *Graph) Resolve(ctx context.Context, identity runtime.Identity) (map[str
8693
}
8794

8895
if err != nil {
96+
if errors.Is(err, ErrNoDirectCredentials) || errors.Is(err, ErrNoIndirectCredentials) {
97+
// not found err
98+
err = errors.Join(ErrNotFound, err)
99+
return nil, fmt.Errorf("failed to resolve credentials for identity %q: %w", identity.String(), err)
100+
}
101+
102+
err = errors.Join(ErrUnknown, err)
89103
return nil, fmt.Errorf("failed to resolve credentials for identity %q: %w", identity.String(), err)
90104
}
91105

bindings/go/credentials/graph_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ func TestResolutionErrors(t *testing.T) {
500500
r.Empty(creds)
501501
r.Error(err)
502502
r.ErrorIs(err, credentials.ErrNoDirectCredentials)
503-
r.ErrorContains(err, fmt.Sprintf("failed to resolve credentials for identity %q: failed to match any node: no direct credentials found in graph", id.String()))
503+
r.ErrorContains(err, fmt.Sprintf("failed to resolve credentials for identity %q: credentials not found", id.String()))
504+
r.ErrorContains(err, "failed to match any node: no direct credentials found in graph")
504505

505506
g, err = credentials.ToGraph(t.Context(), &credentialruntime.Config{}, credentials.Options{
506507
RepositoryPluginProvider: credentials.GetRepositoryPluginFn(func(ctx context.Context, repoType runtime.Typed) (credentials.RepositoryPlugin, error) {
@@ -516,5 +517,6 @@ func TestResolutionErrors(t *testing.T) {
516517
r.Empty(creds)
517518
r.Error(err)
518519
r.ErrorIs(err, credentials.ErrNoIndirectCredentials)
519-
r.ErrorContains(err, fmt.Sprintf("failed to resolve credentials for identity %q: no indirect credentials found in graph", id.String()))
520+
r.ErrorContains(err, fmt.Sprintf("failed to resolve credentials for identity %q: credentials not found", id.String()))
521+
r.ErrorContains(err, "no indirect credentials found in graph")
520522
}

0 commit comments

Comments
 (0)