Skip to content

Commit

Permalink
Cleanup Function Errors (#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmaly committed Apr 22, 2022
1 parent c1e49b9 commit efab12b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions porch/api/porch/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (

PackageRevisionGVR = SchemeGroupVersion.WithResource("packagerevisions")
PackageRevisionResourcesGVR = SchemeGroupVersion.WithResource("packagerevisionresources")
FunctionGVR = SchemeGroupVersion.WithResource("functions")
)

func init() {
Expand Down
13 changes: 8 additions & 5 deletions porch/apiserver/pkg/registry/porch/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package porch

import (
"context"
"errors"
"fmt"
"strings"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
"github.com/GoogleContainerTools/kpt/porch/engine/pkg/engine"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -90,20 +90,23 @@ func (f *functions) List(ctx context.Context, options *metainternalversion.ListO
func (f *functions) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
var repositoryKey client.ObjectKey
if ns, ok := request.NamespaceFrom(ctx); !ok {
return nil, errors.New("namespace is required")
return nil, apierrors.NewBadRequest("namespace must be specified")
} else {
repositoryKey.Namespace = ns
}

if fn, err := parseFunctionName(name); err != nil {
return nil, err
return nil, apierrors.NewNotFound(v1alpha1.FunctionGVR.GroupResource(), name)
} else {
repositoryKey.Name = fn.repository
}

var repository configapi.Repository
if err := f.coreClient.Get(ctx, repositoryKey, &repository); err != nil {
return nil, fmt.Errorf("cannot find repository %q", repositoryKey)
if apierrors.IsNotFound(err) {
return nil, apierrors.NewNotFound(v1alpha1.FunctionGVR.GroupResource(), name)
}
return nil, fmt.Errorf("error getting repository %s: %w", repositoryKey, err)
}

// TODO: implement get to avoid listing
Expand All @@ -118,7 +121,7 @@ func (f *functions) Get(ctx context.Context, name string, options *metav1.GetOpt
}
}

return nil, fmt.Errorf("function %s not found", name)
return nil, apierrors.NewNotFound(v1alpha1.FunctionGVR.GroupResource(), name)
}

type functionName struct {
Expand Down

0 comments on commit efab12b

Please sign in to comment.