Skip to content

Commit

Permalink
fix: use go stdlib error wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
notanatol committed Jan 11, 2024
1 parent 0624ea4 commit 65bb54c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
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) {
}
}
})

})
}
}

0 comments on commit 65bb54c

Please sign in to comment.