Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ func isInterface[T any](err error) bool {
}
}
return false
case interface{ Cause() error }:
err = x.Cause()
if err == nil {
return false
}
default:
return false
}
Expand Down
41 changes: 41 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func TestInvalidArgument(t *testing.T) {
&errInvalidArgument{},
&customInvalidArgument{},
&wrappedInvalidArgument{errors.New("invalid parameter")},
&customMessage{err: ErrInvalidArgument, msg: "custom_message"},
&causerOnlyError{ErrInvalidArgument},
&unwrapperOnlyError{ErrInvalidArgument},
&multiError{errors.New("invalid parameter"), ErrNotFound, ErrInvalidArgument},
} {
if !IsInvalidArgument(match) {
t.Errorf("error did not match invalid argument: %#v", match)
Expand Down Expand Up @@ -203,3 +207,40 @@ func (*customInvalidArgument) InvalidParameter() {}
type wrappedInvalidArgument struct{ error }

func (*wrappedInvalidArgument) InvalidParameter() {}

// causerOnlyError implements Causer interface, not Wrapper.
type causerOnlyError struct {
error
}

func (c *causerOnlyError) Error() string {
return c.error.Error()
}

func (c *causerOnlyError) Cause() error {
return c.error
}

// unwrapperOnlyError implements Wrapper interface, not Causer.
type unwrapperOnlyError struct {
error
}

func (c *unwrapperOnlyError) Error() string {
return c.error.Error()
}

func (c *unwrapperOnlyError) Unwrap() error {
return c.error
}

// multiError implements Wrapper interface, not Causer.
type multiError []error

func (c *multiError) Error() string {
return "multi error"
}

func (c *multiError) Unwrap() []error {
return *c
}