Skip to content

Commit

Permalink
Return nil on empty sink
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Sep 17, 2024
1 parent f8555e5 commit 7410da1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/flags/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (i *SinkFlags) Parse(namespace string) (*sink.Reference, error) {
func (i *SinkFlags) ResolveSink(ctx context.Context, knclient clientdynamic.KnDynamicClient, namespace string) (*duckv1.Destination, error) {
s, err := i.Parse(namespace)
if err != nil {
if errors.Is(err, sink.ErrSinkIsRequired) {
// returns nil, if sink isn't provided to keep the current contract
return nil, nil
}
return nil, err
}
var dest *duckv1.Destination
Expand Down
6 changes: 4 additions & 2 deletions pkg/commands/flags/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func TestResolve(t *testing.T) {
Name: "foo",
}}, ""},
{"absent:foo", nil, "absents \"foo\" not found"},
{"", nil, ""},
}
dynamicClient := dynamicfake.CreateFakeKnDynamicClient(
"default",
Expand All @@ -171,11 +172,12 @@ func TestResolve(t *testing.T) {
t.Run(c.sink, func(t *testing.T) {
sf := &flags.SinkFlags{Sink: c.sink}
result, err := sf.ResolveSink(context.Background(), dynamicClient, "default")
if c.destination != nil {
if c.errContents == "" {
assert.DeepEqual(t, result, c.destination)
assert.NilError(t, err)
assert.Equal(t, c.errContents, "")
} else {
assert.Check(t, err != nil && err.Error() != "",
"error ins't empty")
assert.ErrorContains(t, err, c.errContents)
}
})
Expand Down
36 changes: 36 additions & 0 deletions pkg/util/errors/cause_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors_test

import (
"fmt"
"syscall"
"testing"

"knative.dev/client/pkg/util/errors"
)

func TestCauseOf(t *testing.T) {
errExample := errors.New("example error")
want := syscall.EINVAL
err := fmt.Errorf("%w: %w", errExample, want)

got := errors.CauseOf(err, errExample)
if !errors.Is(got, want) {
t.Errorf("got error %v, want %v", got, want)
}
}
32 changes: 32 additions & 0 deletions pkg/util/errors/wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors

import "emperror.dev/errors"

// Is reports whether any error in err's chain matches target.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func Is(err, target error) bool {
return errors.Is(err, target)
}

// New returns a new error annotated with stack trace at the point New is called.
func New(msg string) error {
return errors.New(msg)
}

0 comments on commit 7410da1

Please sign in to comment.