From 5be055d4d73209c0c2ff8c828bdde31ff766bfa2 Mon Sep 17 00:00:00 2001 From: Terry Howe Date: Thu, 11 Sep 2025 11:02:51 -0600 Subject: [PATCH] chore: handle specific errors Signed-off-by: Terry Howe --- pkg/registry/authorizer.go | 3 ++- pkg/registry/authorizer_test.go | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pkg/registry/authorizer.go b/pkg/registry/authorizer.go index cbae9df98..6d8dd49a0 100644 --- a/pkg/registry/authorizer.go +++ b/pkg/registry/authorizer.go @@ -97,7 +97,8 @@ func (a *Authorizer) Do(originalReq *http.Request) (*http.Response, error) { a.setAttemptBearerAuthentication(false) return resp, nil } - if !strings.Contains(err.Error(), "response status code 40") { + if !strings.Contains(err.Error(), "response status code 401") && + !strings.Contains(err.Error(), "response status code 403") { return nil, err } } diff --git a/pkg/registry/authorizer_test.go b/pkg/registry/authorizer_test.go index d0267803b..a1cba065e 100644 --- a/pkg/registry/authorizer_test.go +++ b/pkg/registry/authorizer_test.go @@ -18,8 +18,10 @@ package registry import ( "context" + "errors" "net/http" "net/http/httptest" + "strings" "sync" "testing" @@ -295,3 +297,49 @@ func TestAuthorizer_ConcurrentAccess(t *testing.T) { wg.Wait() } + +func TestAuthorizer_Do_StatusCodeErrorChecking(t *testing.T) { + tests := []struct { + name string + errorMsg string + shouldRetry bool + description string + }{ + { + name: "retry on 401 error", + errorMsg: "response status code 401", + shouldRetry: true, + description: "401 errors should trigger retry logic", + }, + { + name: "retry on 403 error", + errorMsg: "response status code 403", + shouldRetry: true, + description: "403 errors should trigger retry logic", + }, + { + name: "no retry on 404 error", + errorMsg: "response status code 404", + shouldRetry: false, + description: "404 errors should not trigger retry logic", + }, + { + name: "no retry on 500 error", + errorMsg: "response status code 500", + shouldRetry: false, + description: "500 errors should not trigger retry logic", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := errors.New(tt.errorMsg) + + should401Retry := strings.Contains(err.Error(), "response status code 401") + should403Retry := strings.Contains(err.Error(), "response status code 403") + actualShouldRetry := should401Retry || should403Retry + + assert.Equal(t, tt.shouldRetry, actualShouldRetry, tt.description) + }) + } +}