Skip to content

Commit

Permalink
Merge pull request #7 from MisterMX/feat/get-compose-error-not-found
Browse files Browse the repository at this point in the history
feat!: Return not-found error in req.GetCompose
  • Loading branch information
MisterMX authored Feb 14, 2024
2 parents 9bd8559 + c50d6f1 commit d89f4d0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ type errNotFound struct {
name string
}

func NewErrorNotFound(name string) error {
return &errNotFound{name: name}
}

func (e errNotFound) Error() string {
return fmt.Sprintf("not found: %s", e.name)
}

func IsNotFoundError(err error) bool {
func IsErrorNotFound(err error) bool {
return errors.As(err, &errNotFound{})
}
4 changes: 2 additions & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/pkg/errors"
)

func TestIsNotFoundError(t *testing.T) {
func TestIsErrorNotFound(t *testing.T) {
type args struct {
err error
}
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestIsNotFoundError(t *testing.T) {
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res := IsNotFoundError(tc.err)
res := IsErrorNotFound(tc.err)
if res != tc.want.isNotFound {
t.Errorf("Expected %v but got %v", tc.want.isNotFound, res)
}
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *RunServerFunctionRequest) GetComposed(name string, target runtime.Objec
resources := r.Req.GetObserved().GetResources()
res, exists := resources[name]
if !exists {
return nil
return NewErrorNotFound(name)
}
return resource.AsObject(res.GetResource(), target)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (r *RunServerFunctionResponse) SetComposed(name string, o runtime.Object, m
func (r *RunServerFunctionResponse) GetComposed(name string, target runtime.Object) error {
state, exists := r.DesiredComposed[name]
if !exists {
return errNotFound{name: name}
return NewErrorNotFound(name)
}
return resource.AsObject(state.Resource, target)
}
Expand Down
4 changes: 2 additions & 2 deletions server_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type ServerFunctionRequest interface {
// GetComposed copies the current state of the composed resource identified
// by the given name.
//
// If a no composed resource with the given name exists, target remains
// unchanged.
// If a no composed resource with the given name exists, it returns a
// not-found error that be checked with [IsErrorNotFound]
GetComposed(name string, target runtime.Object) error
}

Expand Down

0 comments on commit d89f4d0

Please sign in to comment.