github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/internal/errors/resolver/pkg.go (about) 1 // Copyright 2021 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package resolver 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/GoogleContainerTools/kpt/internal/errors" 22 "github.com/GoogleContainerTools/kpt/internal/pkg" 23 kptfile "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" 24 ) 25 26 //nolint:gochecknoinits 27 func init() { 28 AddErrorResolver(&pkgErrorResolver{}) 29 } 30 31 // pkgErrorResolver is an implementation of the ErrorResolver interface 32 // that can produce error messages for errors of the pkg.KptfileError type. 33 type pkgErrorResolver struct{} 34 35 func (*pkgErrorResolver) Resolve(err error) (ResolvedResult, bool) { 36 var kptfileError *pkg.KptfileError 37 if errors.As(err, &kptfileError) { 38 path := kptfileError.Path 39 40 return resolveNestedErr(kptfileError, path.String()) 41 } 42 43 var remoteKptfileError *pkg.RemoteKptfileError 44 if errors.As(err, &remoteKptfileError) { 45 path := remoteKptfileError.RepoSpec.RepoRef() 46 47 return resolveNestedErr(remoteKptfileError, path) 48 } 49 50 var validateError *kptfile.ValidateError 51 if errors.As(err, &validateError) { 52 return ResolvedResult{ 53 Message: validateError.Error(), 54 }, true 55 } 56 57 return ResolvedResult{}, false 58 } 59 60 func resolveNestedErr(err error, path string) (ResolvedResult, bool) { 61 if errors.Is(err, os.ErrNotExist) { 62 msg := fmt.Sprintf("Error: No Kptfile found at %q.", path) 63 64 return ResolvedResult{ 65 Message: msg, 66 }, true 67 } 68 69 var deprecatedv1alpha1KptfileError *pkg.DeprecatedKptfileError 70 if errors.As(err, &deprecatedv1alpha1KptfileError) && 71 deprecatedv1alpha1KptfileError.Version == "v1alpha1" { 72 msg := fmt.Sprintf("Error: Kptfile at %q has an old version (%q) of the Kptfile schema.\n", path, deprecatedv1alpha1KptfileError.Version) 73 msg += "Please update the package to the latest format by following https://kpt.dev/installation/migration." 74 75 return ResolvedResult{ 76 Message: msg, 77 }, true 78 } 79 80 var deprecatedv1alpha2KptfileError *pkg.DeprecatedKptfileError 81 if errors.As(err, &deprecatedv1alpha2KptfileError) && 82 deprecatedv1alpha2KptfileError.Version == "v1alpha2" { 83 msg := fmt.Sprintf("Error: Kptfile at %q has an old version (%q) of the Kptfile schema.\n", path, deprecatedv1alpha2KptfileError.Version) 84 msg += "Please run \"kpt fn eval <PKG_PATH> -i gcr.io/kpt-fn/fix:v0.2 --include-meta-resources\" to upgrade the package and retry." 85 86 return ResolvedResult{ 87 Message: msg, 88 }, true 89 } 90 91 var unknownKptfileResourceError *pkg.UnknownKptfileResourceError 92 if errors.As(err, &unknownKptfileResourceError) { 93 msg := fmt.Sprintf("Error: Kptfile at %q has an unknown resource type (%q).", path, unknownKptfileResourceError.GVK.String()) 94 return ResolvedResult{ 95 Message: msg, 96 }, true 97 } 98 99 msg := fmt.Sprintf("Error: Kptfile at %q can't be read.", path) 100 if err != nil { 101 var kptFileError *pkg.KptfileError 102 if errors.As(err, &kptFileError) { 103 if kptFileError.Err != nil { 104 msg += fmt.Sprintf("\n\nDetails:\n%v", kptFileError.Err) 105 } 106 } else { 107 msg += fmt.Sprintf("\n\nDetails:\n%v", err) 108 } 109 } 110 111 return ResolvedResult{ 112 Message: msg, 113 }, true 114 }