Skip to content

Commit

Permalink
Return user friendly error when package doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmadhankumar committed Sep 22, 2023
1 parent 786f84b commit 5fb04ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/apiserver/registry/datapackaging/package_crd_rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
}

}
Expand Down
19 changes: 18 additions & 1 deletion pkg/apiserver/registry/datapackaging/package_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 5fb04ee

Please sign in to comment.