From f8cc0056b4c5b27aad4993a0487ec849450a3e56 Mon Sep 17 00:00:00 2001 From: rcmadhankumar Date: Wed, 13 Sep 2023 19:49:09 +0530 Subject: [PATCH] Return user friendly error when package doesn't exist --- .../datapackaging/package_storage_client.go | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/apiserver/registry/datapackaging/package_storage_client.go b/pkg/apiserver/registry/datapackaging/package_storage_client.go index f7c5ab34f2..5d40b6bbba 100644 --- a/pkg/apiserver/registry/datapackaging/package_storage_client.go +++ b/pkg/apiserver/registry/datapackaging/package_storage_client.go @@ -7,6 +7,7 @@ import ( "context" "encoding/base32" "fmt" + "regexp" "strings" internalpkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/internalpackaging/v1alpha1" @@ -152,7 +153,25 @@ func (t PackageTranslator) ToExternalWatcher(intObjWatcher watch.Interface, fiel } func (t PackageTranslator) ToExternalError(err error) error { - // TODO: implement + 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] + } + } + + packageName, err := t.ToExternalName(internalPackageName) + if err != nil { + return err + } + return errors.NewNotFound(datapkgingv1alpha1.Resource("package"), packageName) + } + return err }