From b95f33c90e6d83eef476620063756c2ae770cec2 Mon Sep 17 00:00:00 2001 From: yugo kobayashi Date: Fri, 26 Aug 2022 14:38:51 +0000 Subject: [PATCH] fix error handling --- api/internal/plugins/fnplugin/fnplugin.go | 4 ++-- api/internal/plugins/loader/loader.go | 4 ++-- api/krusty/fnplugin_test.go | 4 ++-- kyaml/fn/runtime/runtimeutil/functiontypes.go | 8 ++++++-- kyaml/runfn/runfn.go | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/api/internal/plugins/fnplugin/fnplugin.go b/api/internal/plugins/fnplugin/fnplugin.go index 67d1803a355..bf9d794d8fb 100644 --- a/api/internal/plugins/fnplugin/fnplugin.go +++ b/api/internal/plugins/fnplugin/fnplugin.go @@ -54,11 +54,11 @@ func resourceToRNode(res *resource.Resource) (*yaml.RNode, error) { func GetFunctionSpec(res *resource.Resource) (*runtimeutil.FunctionSpec, error) { rnode, err := resourceToRNode(res) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, fmt.Errorf("could not convert resource to RNode: %w", err) } functionSpec, err := runtimeutil.GetFunctionSpec(rnode) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, fmt.Errorf("failed to get FunctionSpec: %w", err) } return functionSpec, nil } diff --git a/api/internal/plugins/loader/loader.go b/api/internal/plugins/loader/loader.go index 0d1d601c699..26441ed51c6 100644 --- a/api/internal/plugins/loader/loader.go +++ b/api/internal/plugins/loader/loader.go @@ -58,11 +58,11 @@ func (l *Loader) LoadGenerators( for _, res := range rm.Resources() { g, err := l.LoadGenerator(ldr, v, res) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, fmt.Errorf("failed to load generator: %w", err) } generatorOrigin, err := resource.OriginFromCustomPlugin(res) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, fmt.Errorf("failed to get origin from CustomPlugin: %w", err) } result = append(result, &resmap.GeneratorWithProperties{Generator: g, Origin: generatorOrigin}) } diff --git a/api/krusty/fnplugin_test.go b/api/krusty/fnplugin_test.go index 173d9e4b1ea..a4be95d5575 100644 --- a/api/krusty/fnplugin_test.go +++ b/api/krusty/fnplugin_test.go @@ -737,7 +737,7 @@ generators: fSys, tmpDir.String()) assert.Error(t, err) - assert.Contains(t, err.Error(), "loading generator plugins: plugin RenderHelmChart."+ + assert.Contains(t, err.Error(), "loading generator plugins: failed to load generator: plugin RenderHelmChart."+ "v1alpha1.[noGrp]/demo.[noNs] with mount path '/tmp/dir' is not permitted; mount paths must"+ " be relative to the current kustomization directory") } @@ -770,7 +770,7 @@ generators: fSys, tmpDir.String()) assert.Error(t, err) - assert.Contains(t, err.Error(), "loading generator plugins: plugin RenderHelmChart."+ + assert.Contains(t, err.Error(), "loading generator plugins: failed to load generator: plugin RenderHelmChart."+ "v1alpha1.[noGrp]/demo.[noNs] with mount path './tmp/../../dir' is not permitted; mount paths must "+ "be under the current kustomization directory") } diff --git a/kyaml/fn/runtime/runtimeutil/functiontypes.go b/kyaml/fn/runtime/runtimeutil/functiontypes.go index 80f6197cefd..6311b999954 100644 --- a/kyaml/fn/runtime/runtimeutil/functiontypes.go +++ b/kyaml/fn/runtime/runtimeutil/functiontypes.go @@ -206,9 +206,13 @@ func GetFunctionSpec(n *yaml.RNode) (*FunctionSpec, error) { if err != nil { return nil, nil } - if fn, err := getFunctionSpecFromAnnotation(n, meta); err != nil { + + fn, err := getFunctionSpecFromAnnotation(n, meta) + if err != nil { return nil, err - } else if fn != nil { + } + + if fn != nil { return fn, nil } diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index e1564581b50..6b36d7d3a9d 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -310,7 +310,7 @@ func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) ( api := fns[i] spec, err := runtimeutil.GetFunctionSpec(api) if err != nil { - return nil, fmt.Errorf("%w", err) + return nil, fmt.Errorf("failed to get FunctionSpec: %w", err) } if spec == nil { // resource doesn't have function spec