Return nil on empty sink

This commit is contained in:
Chris Suszyński 2024-09-17 14:28:00 +02:00
parent f8555e5fa1
commit 7410da1814
No known key found for this signature in database
GPG Key ID: 97CCAEB0D718922B
4 changed files with 76 additions and 2 deletions

View File

@ -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

View File

@ -161,6 +161,7 @@ func TestResolve(t *testing.T) {
Name: "foo",
}}, ""},
{"absent:foo", nil, "absents \"foo\" not found"},
{"", nil, ""},
}
dynamicClient := dynamicfake.CreateFakeKnDynamicClient(
"default",
@ -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)
}
})

View File

@ -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
pkg/util/errors/wrap.go Normal file
View File

@ -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)
}