Skip to content

Commit

Permalink
Add ShardErrors.Unwrap() support
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Pracucci <marco@pracucci.com>
  • Loading branch information
pracucci committed Jul 11, 2024
1 parent a5f2b71 commit 831cca0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/kadm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ func (e *ShardErrors) Error() string {
}
return fmt.Sprintf("request %s has %d separate shard errors, first: %s", e.Name, len(e.Errs), e.Errs[0].Err)
}

// Unwrap returns the underlying errors.
func (e *ShardErrors) Unwrap() []error {
unwrapped := make([]error, 0, len(e.Errs))

for _, shardErr := range e.Errs {
unwrapped = append(unwrapped, shardErr.Err)
}

return unwrapped
}
23 changes: 23 additions & 0 deletions pkg/kadm/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kadm

import (
"context"
"errors"
"testing"
)

func TestShardErrors_Unwrap(t *testing.T) {
err1 := errors.New("test error 1")
err2 := errors.New("test error 2")

errs := &ShardErrors{Errs: []ShardError{{Err: err1}, {Err: context.Canceled}}}
if !errors.Is(errs, err1) {
t.Errorf("ShardErrors does not match error %v", err1)
}
if !errors.Is(errs, context.Canceled) {
t.Errorf("ShardErrors does not match error %v", context.Canceled)
}
if errors.Is(errs, err2) {
t.Errorf("ShardErrors matches error %v but it not expected to match it", err2)
}
}

0 comments on commit 831cca0

Please sign in to comment.