Skip to content

Commit 9d77641

Browse files
authored
Merge pull request #987 from fluxcd/chart-version-build-err
helm: typed errors from GetChartVersion()
2 parents 0f7a263 + 869a9df commit 9d77641

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

internal/helm/chart/builder_remote.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,17 @@ func (b *remoteChartBuilder) downloadFromRepository(ctx context.Context, remote
128128
// Get the current version for the RemoteReference
129129
cv, err := remote.GetChartVersion(remoteRef.Name, remoteRef.Version)
130130
if err != nil {
131+
var reason BuildErrorReason
132+
switch err.(type) {
133+
case *repository.ErrReference:
134+
reason = ErrChartReference
135+
case *repository.ErrExternal:
136+
reason = ErrChartPull
137+
default:
138+
reason = ErrUnknown
139+
}
131140
err = fmt.Errorf("failed to get chart version for remote reference: %w", err)
132-
return nil, nil, &BuildError{Reason: ErrChartReference, Err: err}
141+
return nil, nil, &BuildError{Reason: reason, Err: err}
133142
}
134143

135144
// Verify the chart if necessary

internal/helm/repository/chart_repository.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,17 @@ func newChartRepository() *ChartRepository {
158158
func (r *ChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
159159
// See if we already have the index in cache or try to load it.
160160
if err := r.StrategicallyLoadIndex(); err != nil {
161-
return nil, err
161+
return nil, &ErrExternal{Err: err}
162162
}
163163

164+
cv, err := r.getChartVersion(name, ver)
165+
if err != nil {
166+
return nil, &ErrReference{Err: err}
167+
}
168+
return cv, nil
169+
}
170+
171+
func (r *ChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
164172
r.RLock()
165173
defer r.RUnlock()
166174

internal/helm/repository/errors.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2022 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package repository
18+
19+
// ErrReference indicate invalid chart reference.
20+
type ErrReference struct {
21+
Err error
22+
}
23+
24+
// Error implements the error interface.
25+
func (er *ErrReference) Error() string {
26+
return er.Err.Error()
27+
}
28+
29+
// Unwrap returns the underlying error.
30+
func (er *ErrReference) Unwrap() error {
31+
return er.Err
32+
}
33+
34+
// ErrExternal is a generic error for errors related to external API calls.
35+
type ErrExternal struct {
36+
Err error
37+
}
38+
39+
// Error implements the error interface.
40+
func (ee *ErrExternal) Error() string {
41+
return ee.Err.Error()
42+
}
43+
44+
// Unwrap returns the underlying error.
45+
func (ee *ErrExternal) Unwrap() error {
46+
return ee.Err
47+
}

internal/helm/repository/oci_chart_repository.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,14 @@ func NewOCIChartRepository(repositoryURL string, chartRepoOpts ...OCIChartReposi
146146
// stable version will be returned and prerelease versions will be ignored.
147147
// adapted from https://github.com/helm/helm/blob/49819b4ef782e80b0c7f78c30bd76b51ebb56dc8/pkg/downloader/chart_downloader.go#L162
148148
func (r *OCIChartRepository) GetChartVersion(name, ver string) (*repo.ChartVersion, error) {
149+
cv, err := r.getChartVersion(name, ver)
150+
if err != nil {
151+
return nil, &ErrExternal{Err: err}
152+
}
153+
return cv, nil
154+
}
149155

156+
func (r *OCIChartRepository) getChartVersion(name, ver string) (*repo.ChartVersion, error) {
150157
cpURL := r.URL
151158
cpURL.Path = path.Join(cpURL.Path, name)
152159

0 commit comments

Comments
 (0)