From 4291bdc1d894f754bc72b389e959529264bd9b15 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_crd_rest_test.go | 5 +++-- .../datapackaging/package_storage_client.go | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/apiserver/registry/datapackaging/package_crd_rest_test.go b/pkg/apiserver/registry/datapackaging/package_crd_rest_test.go index a1bdb167bc..3864213171 100644 --- a/pkg/apiserver/registry/datapackaging/package_crd_rest_test.go +++ b/pkg/apiserver/registry/datapackaging/package_crd_rest_test.go @@ -146,6 +146,7 @@ func TestPackageVersionGetPresentInOnlyNS(t *testing.T) { func TestPackageVersionGetNotFound(t *testing.T) { namespacedPackageVersion := excludedNonGlobalIntPackageVersion() name := namespacedPackageName + expectedError := "package.data.packaging.carvel.dev \"" + name + "\" not found" internalClient := fake.NewSimpleClientset(namespacedPackageVersion) fakeCoreClient := k8sfake.NewSimpleClientset(namespace()) @@ -157,8 +158,8 @@ func TestPackageVersionGetNotFound(t *testing.T) { t.Fatalf("Expected get operation to fail, but it didn't") } - if !errors.IsNotFound(err) { - t.Fatalf("Expected a not found error, got: %v", err) + if !errors.IsNotFound(err) || err.Error() != expectedError { + t.Fatalf("Expected error : %s, got: %v", expectedError, err) } } diff --git a/pkg/apiserver/registry/datapackaging/package_storage_client.go b/pkg/apiserver/registry/datapackaging/package_storage_client.go index f7c5ab34f2..9cf2e2486f 100644 --- a/pkg/apiserver/registry/datapackaging/package_storage_client.go +++ b/pkg/apiserver/registry/datapackaging/package_storage_client.go @@ -152,7 +152,24 @@ func (t PackageTranslator) ToExternalWatcher(intObjWatcher watch.Interface, fiel } func (t PackageTranslator) ToExternalError(err error) error { - // TODO: implement + statusErr := err.(*errors.StatusError) + + 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 + } + + 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 }