Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redundant error unwrap method #4535

Merged
merged 1 commit into from
Jan 19, 2024
Merged
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
21 changes: 4 additions & 17 deletions pkg/replicas/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,7 @@ import (
// then the probability of Swarmageddon is less than 0.000001
// assuming the error rate of chunk retrievals stays below the level expressed
// as depth by the publisher.
type ErrSwarmageddon struct {
error
}

func (err *ErrSwarmageddon) Unwrap() []error {
if err == nil || err.error == nil {
return nil
}
var uwe interface{ Unwrap() []error }
if !errors.As(err.error, &uwe) {
return nil
}
return uwe.Unwrap()
}
var ErrSwarmageddon = errors.New("swarmageddon has begun")

// getter is the private implementation of storage.Getter, an interface for
// retrieving chunks. This getter embeds the original simple chunk getter and extends it
Expand Down Expand Up @@ -69,7 +56,7 @@ func (g *getter) Get(ctx context.Context, addr swarm.Address) (ch swarm.Chunk, e
resultC := make(chan swarm.Chunk)
// errc collects the errors
errc := make(chan error, 17)
var errs []error
var errs error
errcnt := 0

// concurrently call to retrieve chunk using original CAC address
Expand Down Expand Up @@ -108,10 +95,10 @@ func (g *getter) Get(ctx context.Context, addr swarm.Address) (ch swarm.Chunk, e
return chunk, nil

case err = <-errc:
errs = append(errs, err)
errs = errors.Join(errs, err)
errcnt++
if errcnt > total {
return nil, &ErrSwarmageddon{errors.Join(errs...)}
return nil, errors.Join(ErrSwarmageddon, errs)
}

// ticker switches on the address channel
Expand Down
14 changes: 3 additions & 11 deletions pkg/replicas/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,11 @@ func TestGetter(t *testing.T) {
}

t.Run("returns correct error", func(t *testing.T) {
var esg *replicas.ErrSwarmageddon
if !errors.As(err, &esg) {
if !errors.Is(err, replicas.ErrSwarmageddon) {
t.Fatalf("incorrect error. want Swarmageddon. got %v", err)
}
errs := esg.Unwrap()
for _, err := range errs {
if !errors.Is(err, tc.failure.err) {
t.Fatalf("incorrect error. want it to wrap %v. got %v", tc.failure.err, err)
}
}
if len(errs) != tc.count+1 {
t.Fatalf("incorrect error. want %d. got %d", tc.count+1, len(errs))
if !errors.Is(err, tc.failure.err) {
t.Fatalf("incorrect error. want it to wrap %v. got %v", tc.failure.err, err)
}
})
}
Expand Down Expand Up @@ -265,7 +258,6 @@ func TestGetter(t *testing.T) {
}
}
})

})
}
}
Loading