fix(sdk): Polish the downloader/manager package error return (#8491)

* fix(sdk): Polish the downloader/manager package error return

Close #8471

Signed-off-by: Dong Gang <dong.gang@daocloud.io>

* Modify the repositories validation function `resloveRepoNames` and add a
unit test.

Signed-off-by: Dong Gang <dong.gang@daocloud.io>

* Remove wrong commit

Signed-off-by: Dong Gang <dong.gang@daocloud.io>
pull/8507/head
Holder 5 years ago committed by GitHub
parent 241785c70f
commit ffc3d42f87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -16,6 +16,7 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -65,7 +66,11 @@ func newDependencyBuildCmd(out io.Writer) *cobra.Command {
if client.Verify { if client.Verify {
man.Verify = downloader.VerifyIfPossible man.Verify = downloader.VerifyIfPossible
} }
return man.Build() err := man.Build()
if e, ok := err.(downloader.ErrRepoNotFound); ok {
return fmt.Errorf("%s. Please add the missing repos via 'helm repo add'", e.Error())
}
return err
}, },
} }

@ -42,6 +42,17 @@ import (
"helm.sh/helm/v3/pkg/repo" "helm.sh/helm/v3/pkg/repo"
) )
// ErrRepoNotFound indicates that chart repositories can't be found in local repo cache.
// The value of Repos is missing repos.
type ErrRepoNotFound struct {
Repos []string
}
// Error implements the error interface.
func (e ErrRepoNotFound) Error() string {
return fmt.Sprintf("no repository definition for %s", strings.Join(e.Repos, ", "))
}
// Manager handles the lifecycle of fetching, resolving, and storing dependencies. // Manager handles the lifecycle of fetching, resolving, and storing dependencies.
type Manager struct { type Manager struct {
// Out is used to print warnings and notifications. // Out is used to print warnings and notifications.
@ -411,7 +422,7 @@ Loop:
missing = append(missing, dd.Repository) missing = append(missing, dd.Repository)
} }
if len(missing) > 0 { if len(missing) > 0 {
return errors.Errorf("no repository definition for %s. Please add the missing repos via 'helm repo add'", strings.Join(missing, ", ")) return ErrRepoNotFound{missing}
} }
return nil return nil
} }

@ -360,3 +360,32 @@ func TestBuild_WithRepositoryAlias(t *testing.T) {
Repository: "@test", Repository: "@test",
}) })
} }
func TestErrRepoNotFound_Error(t *testing.T) {
type fields struct {
Repos []string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "OK",
fields: fields{
Repos: []string{"https://charts1.example.com", "https://charts2.example.com"},
},
want: "no repository definition for https://charts1.example.com, https://charts2.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := ErrRepoNotFound{
Repos: tt.fields.Repos,
}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}

Loading…
Cancel
Save