From 5fa6921520c82f7d6444fad612590c1cd22759c6 Mon Sep 17 00:00:00 2001 From: rcmadhankumar Date: Fri, 22 Sep 2023 21:38:18 +0530 Subject: [PATCH] fix --- .../datapackaging/package_storage_client.go | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/pkg/apiserver/registry/datapackaging/package_storage_client.go b/pkg/apiserver/registry/datapackaging/package_storage_client.go index 5d40b6bbb..9cf2e2486 100644 --- a/pkg/apiserver/registry/datapackaging/package_storage_client.go +++ b/pkg/apiserver/registry/datapackaging/package_storage_client.go @@ -7,7 +7,6 @@ import ( "context" "encoding/base32" "fmt" - "regexp" "strings" internalpkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/internalpackaging/v1alpha1" @@ -153,23 +152,22 @@ func (t PackageTranslator) ToExternalWatcher(intObjWatcher watch.Interface, fiel } func (t PackageTranslator) ToExternalError(err error) error { - if errors.IsNotFound(err) { - errStr := err.Error() - regEx := `^internalpackages\.internal\.packaging\.carvel\.dev "(?P[a-z0-9]*)" not found$` - re := regexp.MustCompile(regEx) - match := re.FindStringSubmatch(errStr) - internalPackageName := "" - for i, name := range re.SubexpNames() { - if i != 0 && name == "internalPackageName" { - internalPackageName = match[i] - } - } + statusErr := err.(*errors.StatusError) - packageName, err := t.ToExternalName(internalPackageName) + if statusErr.ErrStatus.Details.Kind == "internalpackages" && statusErr.ErrStatus.Details.Group == internalpkgingv1alpha1.SchemeGroupVersion.Group { + packageName, err := t.ToExternalName(statusErr.ErrStatus.Details.Name) if err != nil { return err } - return errors.NewNotFound(datapkgingv1alpha1.Resource("package"), packageName) + + switch statusErr.ErrStatus.Reason { + case metav1.StatusReasonNotFound: + return errors.NewNotFound(datapkgingv1alpha1.Resource("package"), packageName) + case metav1.StatusReasonAlreadyExists: + return errors.NewAlreadyExists(datapkgingv1alpha1.Resource("package"), packageName) + default: + return err + } } return err