From 5350b03308569a6b778d4b074aa54d973074d795 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:12:34 +0100 Subject: [PATCH] fix: error handling for gitlab variable fetch (#4177) --- pkg/provider/gitlab/gitlab.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/pkg/provider/gitlab/gitlab.go b/pkg/provider/gitlab/gitlab.go index 4e1158ea7..272fa39c9 100644 --- a/pkg/provider/gitlab/gitlab.go +++ b/pkg/provider/gitlab/gitlab.go @@ -225,21 +225,16 @@ func (g *gitlabBase) GetSecret(_ context.Context, ref esv1beta1.ExternalSecretDa data, resp, err := g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts) metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err) if err != nil { - return nil, err - } - - if resp == nil { - return nil, errors.New("gitlab response is nil") - } - - if !isEmptyOrWildcard(g.store.Environment) && resp.StatusCode == http.StatusNotFound { - vopts.Filter.EnvironmentScope = "*" - data, resp, err = g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts) - metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err) - } - - if resp.StatusCode >= 400 && resp.StatusCode != http.StatusNotFound { - return nil, fmt.Errorf("gitlab response status code was not OK: %d", resp.StatusCode) + if resp != nil && resp.StatusCode == http.StatusNotFound && !isEmptyOrWildcard(g.store.Environment) { + vopts.Filter.EnvironmentScope = "*" + data, resp, err = g.projectVariablesClient.GetVariable(g.store.ProjectID, ref.Key, vopts) + metrics.ObserveAPICall(constants.ProviderGitLab, constants.CallGitLabProjectVariableGet, err) + if err != nil || resp == nil { + return nil, fmt.Errorf("error getting variable %s from GitLab: %w", ref.Key, err) + } + } else { + return nil, err + } } err = g.ResolveGroupIds()