Skip to content

Commit 864905d

Browse files
authored
Merge pull request #88 from libp2p/one-multierr-pkg
Consolidate multi-error packages by choosing one
2 parents 7bb219f + e927fac commit 864905d

8 files changed

+34
-33
lines changed

bootstrap_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"testing"
77

8-
"github.com/hashicorp/errwrap"
98
"github.com/libp2p/go-libp2p/core/routing"
109
)
1110

@@ -152,7 +151,7 @@ func TestBootstrapErr(t *testing.T) {
152151
err := d.Bootstrap(ctx)
153152
t.Log(err)
154153
for _, s := range []string{"err1", "err2", "err3", "err4"} {
155-
if !errwrap.Contains(err, s) {
154+
if !errContains(err, s) {
156155
t.Errorf("expecting error to contain '%s'", s)
157156
}
158157
}

composed.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package routinghelpers
33
import (
44
"context"
55

6-
"github.com/hashicorp/go-multierror"
76
"github.com/ipfs/go-cid"
87
ci "github.com/libp2p/go-libp2p/core/crypto"
98
"github.com/libp2p/go-libp2p/core/peer"
109
"github.com/libp2p/go-libp2p/core/routing"
10+
"go.uber.org/multierr"
1111
)
1212

1313
// Compose composes the components into a single router. Not specifying a
@@ -133,13 +133,13 @@ func (cr *Compose) Bootstrap(ctx context.Context) (err error) {
133133
}
134134
}
135135

136-
var me multierror.Error
136+
var errs error
137137
for b := range routers {
138138
if err := b.Bootstrap(ctx); err != nil {
139-
me.Errors = append(me.Errors, err)
139+
errs = multierr.Append(errs, err)
140140
}
141141
}
142-
return me.ErrorOrNil()
142+
return errs
143143
}
144144

145145
var _ routing.Routing = (*Compose)(nil)

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ go 1.23.0
44

55
require (
66
github.com/Jorropo/jsync v1.0.1
7-
github.com/hashicorp/errwrap v1.1.0
8-
github.com/hashicorp/go-multierror v1.1.1
97
github.com/ipfs/go-cid v0.5.0
108
github.com/ipfs/go-log/v2 v2.5.1
119
github.com/libp2p/go-libp2p v0.41.0

go.sum

-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
1616
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1717
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1818
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
19-
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
20-
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
21-
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
22-
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
23-
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
2419
github.com/ipfs/go-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs=
2520
github.com/ipfs/go-block-format v0.2.0/go.mod h1:+jpL11nFx5A/SPpsoBn6Bzkra/zaArfSmsknbPMYgzM=
2621
github.com/ipfs/go-cid v0.5.0 h1:goEKKhaGm0ul11IHA7I6p1GmKz8kEYniqFopaB5Otwg=

parallel.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"sync"
99

1010
"github.com/Jorropo/jsync"
11-
"github.com/hashicorp/go-multierror"
1211
"github.com/ipfs/go-cid"
1312
record "github.com/libp2p/go-libp2p-record"
1413
ci "github.com/libp2p/go-libp2p/core/crypto"
1514
"github.com/libp2p/go-libp2p/core/peer"
1615
"github.com/libp2p/go-libp2p/core/routing"
16+
"go.uber.org/multierr"
1717
)
1818

1919
// Parallel operates on the slice of routers in parallel.
@@ -156,7 +156,7 @@ func (r Parallel) put(do func(routing.Routing) error) error {
156156
case 1:
157157
return errs[0]
158158
default:
159-
return &multierror.Error{Errors: errs}
159+
return multierr.Combine(errs...)
160160
}
161161
}
162162

@@ -273,7 +273,7 @@ func (r Parallel) get(ctx context.Context, do func(routing.Routing) (interface{}
273273
case 1:
274274
return nil, errs[0]
275275
default:
276-
return nil, &multierror.Error{Errors: errs}
276+
return nil, multierr.Combine(errs...)
277277
}
278278
}
279279

@@ -579,26 +579,26 @@ func fewProviders(ctx context.Context, out chan<- peer.AddrInfo, in []<-chan pee
579579

580580
// Bootstrap signals all the sub-routers to bootstrap.
581581
func (r Parallel) Bootstrap(ctx context.Context) error {
582-
var me multierror.Error
582+
var errs error
583583
for _, b := range r.Routers {
584584
if err := b.Bootstrap(ctx); err != nil {
585-
me.Errors = append(me.Errors, err)
585+
errs = multierr.Append(errs, err)
586586
}
587587
}
588-
return me.ErrorOrNil()
588+
return errs
589589
}
590590

591591
// Close closes all sub-routers that implement the io.Closer interface.
592592
func (r Parallel) Close() error {
593-
var me multierror.Error
593+
var errs error
594594
for _, router := range r.Routers {
595595
if closer, ok := router.(io.Closer); ok {
596596
if err := closer.Close(); err != nil {
597-
me.Errors = append(me.Errors, err)
597+
errs = multierr.Append(errs, err)
598598
}
599599
}
600600
}
601-
return me.ErrorOrNil()
601+
return errs
602602
}
603603

604604
var _ routing.Routing = Parallel{}

parallel_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package routinghelpers
33
import (
44
"context"
55
"errors"
6+
"strings"
67
"testing"
78
"time"
89

9-
"github.com/hashicorp/errwrap"
1010
"github.com/ipfs/go-cid"
1111
"github.com/libp2p/go-libp2p/core/peer"
1212
"github.com/libp2p/go-libp2p/core/routing"
1313
mh "github.com/multiformats/go-multihash"
14+
"go.uber.org/multierr"
1415
)
1516

1617
// NOTE: While this test is primarily testing the Parallel combinator, it also
@@ -99,10 +100,10 @@ func TestParallelPutGet(t *testing.T) {
99100
if err := d.PutValue(ctx, "/notsupported/hello", []byte("world")); err != routing.ErrNotSupported {
100101
t.Fatalf("expected ErrNotSupported, got: %s", err)
101102
}
102-
if err := d.PutValue(ctx, "/error/myErr", []byte("world")); !errwrap.Contains(err, "myErr") {
103+
if err := d.PutValue(ctx, "/error/myErr", []byte("world")); !errContains(err, "myErr") {
103104
t.Fatalf("expected error to contain myErr, got: %s", err)
104105
}
105-
if _, err := d.GetValue(ctx, "/error/myErr"); !errwrap.Contains(err, "myErr") {
106+
if _, err := d.GetValue(ctx, "/error/myErr"); !errContains(err, "myErr") {
106107
t.Fatalf("expected error to contain myErr, got: %s", err)
107108
}
108109
if err := d.PutValue(ctx, "/solo/thing", []byte("value")); err != nil {
@@ -129,6 +130,15 @@ func TestParallelPutGet(t *testing.T) {
129130
cancel()
130131
}
131132

133+
func errContains(err error, substr string) bool {
134+
for _, e := range multierr.Errors(err) {
135+
if strings.Contains(e.Error(), substr) {
136+
return true
137+
}
138+
}
139+
return false
140+
}
141+
132142
func TestParallelPutFailure(t *testing.T) {
133143
ctx := context.Background()
134144
router := Parallel{

tiered.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"io"
66

7-
"github.com/hashicorp/go-multierror"
87
"github.com/ipfs/go-cid"
98
record "github.com/libp2p/go-libp2p-record"
109
ci "github.com/libp2p/go-libp2p/core/crypto"
1110
"github.com/libp2p/go-libp2p/core/peer"
1211
"github.com/libp2p/go-libp2p/core/routing"
12+
"go.uber.org/multierr"
1313
)
1414

1515
// Tiered is like the Parallel except that GetValue and FindPeer
@@ -47,7 +47,7 @@ func (r Tiered) get(ctx context.Context, do func(routing.Routing) (interface{},
4747
case 1:
4848
return nil, errs[0]
4949
default:
50-
return nil, &multierror.Error{Errors: errs}
50+
return nil, multierr.Combine(errs...)
5151
}
5252
}
5353

@@ -112,15 +112,15 @@ func (r Tiered) Bootstrap(ctx context.Context) error {
112112

113113
// Close closes all sub-routers that implement the io.Closer interface.
114114
func (r Tiered) Close() error {
115-
var me multierror.Error
115+
var errs error
116116
for _, router := range r.Routers {
117117
if closer, ok := router.(io.Closer); ok {
118118
if err := closer.Close(); err != nil {
119-
me.Errors = append(me.Errors, err)
119+
errs = multierr.Append(errs, err)
120120
}
121121
}
122122
}
123-
return me.ErrorOrNil()
123+
return errs
124124
}
125125

126126
var _ routing.Routing = Tiered{}

tiered_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"testing"
77

8-
"github.com/hashicorp/errwrap"
98
"github.com/ipfs/go-cid"
109
record "github.com/libp2p/go-libp2p-record"
1110
"github.com/libp2p/go-libp2p/core/routing"
@@ -178,11 +177,11 @@ func TestTieredGet(t *testing.T) {
178177
t.Fatal(err)
179178
}
180179

181-
if _, err := d.GetValue(ctx, "/error/myErr"); !errwrap.Contains(err, "myErr") {
180+
if _, err := d.GetValue(ctx, "/error/myErr"); !errContains(err, "myErr") {
182181
t.Fatalf("expected error to contain myErr, got: %s", err)
183182
}
184183

185-
if _, err := (Tiered{Routers: []routing.Routing{d.Routers[1]}}).GetValue(ctx, "/error/myErr"); !errwrap.Contains(err, "myErr") {
184+
if _, err := (Tiered{Routers: []routing.Routing{d.Routers[1]}}).GetValue(ctx, "/error/myErr"); !errContains(err, "myErr") {
186185
t.Fatalf("expected error to contain myErr, got: %s", err)
187186
}
188187

0 commit comments

Comments
 (0)