diff --git a/client/errors.go b/client/errors.go index 7e5a69d3..c31e448d 100644 --- a/client/errors.go +++ b/client/errors.go @@ -1,11 +1,24 @@ package client -import apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" +import ( + "errors" + + apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" +) + +// unwraps err using errors.Unwrap and returns the result. +func unwrapErr(err error) error { + for e := errors.Unwrap(err); e != nil; e = errors.Unwrap(err) { + err = e + } + + return err +} // IsErrContainerNotFound checks if err corresponds to NeoFS status -// return corresponding to missing container. +// return corresponding to missing container. Supports wrapped errors. func IsErrContainerNotFound(err error) bool { - switch err.(type) { + switch unwrapErr(err).(type) { default: return false case @@ -16,9 +29,9 @@ func IsErrContainerNotFound(err error) bool { } // IsErrObjectNotFound checks if err corresponds to NeoFS status -// return corresponding to missing object. +// return corresponding to missing object. Supports wrapped errors. func IsErrObjectNotFound(err error) bool { - switch err.(type) { + switch unwrapErr(err).(type) { default: return false case @@ -29,9 +42,9 @@ func IsErrObjectNotFound(err error) bool { } // IsErrObjectAlreadyRemoved checks if err corresponds to NeoFS status -// return corresponding to already removed object. +// return corresponding to already removed object. Supports wrapped errors. func IsErrObjectAlreadyRemoved(err error) bool { - switch err.(type) { + switch unwrapErr(err).(type) { default: return false case diff --git a/client/errors_test.go b/client/errors_test.go index bc3a5ee7..2924abd4 100644 --- a/client/errors_test.go +++ b/client/errors_test.go @@ -1,6 +1,7 @@ package client_test import ( + "fmt" "testing" "github.com/nspcc-dev/neofs-sdk-go/client" @@ -39,6 +40,7 @@ func TestErrors(t *testing.T) { for i := range tc.errs { require.True(t, tc.check(tc.errs[i]), tc.errs[i]) + require.True(t, tc.check(fmt.Errorf("some context: %w", tc.errs[i])), tc.errs[i]) } } }