Skip to content

Commit

Permalink
Merge pull request #775 from pracucci/add-ShardErrors-unwrap
Browse files Browse the repository at this point in the history
Add ShardErrors.Unwrap() support
  • Loading branch information
twmb authored Jul 29, 2024
2 parents 36265e0 + 831cca0 commit d5978bd
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 d5978bd

Please sign in to comment.