Skip to content

Commit ffba2a8

Browse files
authored
refactor: use errors.Join to wrap multiple errors (#1322)
1 parent 024e230 commit ffba2a8

File tree

5 files changed

+14
-29
lines changed

5 files changed

+14
-29
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/golang/protobuf v1.5.4
1414
github.com/google/uuid v1.6.0
1515
github.com/hamba/avro/v2 v2.26.0
16-
github.com/hashicorp/go-multierror v1.1.1
1716
github.com/klauspost/compress v1.17.9
1817
github.com/onsi/ginkgo v1.16.5
1918
github.com/onsi/gomega v1.19.0
@@ -61,7 +60,6 @@ require (
6160
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
6261
github.com/gogo/protobuf v1.3.2 // indirect
6362
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
64-
github.com/hashicorp/errwrap v1.1.0 // indirect
6563
github.com/inconshreveable/mousetrap v1.0.1 // indirect
6664
github.com/json-iterator/go v1.1.12 // indirect
6765
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@ github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8
213213
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
214214
github.com/hamba/avro/v2 v2.26.0 h1:IaT5l6W3zh7K67sMrT2+RreJyDTllBGVJm4+Hedk9qE=
215215
github.com/hamba/avro/v2 v2.26.0/go.mod h1:I8glyswHnpED3Nlx2ZdUe+4LJnCOOyiCzLMno9i/Uu0=
216-
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
217-
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
218-
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
219-
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
220-
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
221216
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
222217
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
223218
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

pulsar/error.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222

2323
proto "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
24-
"github.com/hashicorp/go-multierror"
2524
)
2625

2726
// Result used to represent pulsar processing is an alias of type int.
@@ -255,10 +254,3 @@ func getErrorFromServerError(serverError *proto.ServerError) error {
255254
return newError(UnknownError, serverError.String())
256255
}
257256
}
258-
259-
// joinErrors can join multiple errors into one error, and the returned error can be tested by errors.Is()
260-
// we use github.com/hashicorp/go-multierror instead of errors.Join() of Go 1.20 so that we can compile pulsar
261-
// go client with go versions that newer than go 1.13
262-
func joinErrors(errs ...error) error {
263-
return multierror.Append(nil, errs...)
264-
}

pulsar/error_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Test_joinErrors(t *testing.T) {
2828
err1 := errors.New("err1")
2929
err2 := errors.New("err2")
3030
err3 := errors.New("err3")
31-
err := joinErrors(ErrInvalidMessage, err1, err2)
31+
err := errors.Join(ErrInvalidMessage, err1, err2)
3232
assert.True(t, errors.Is(err, ErrInvalidMessage))
3333
assert.True(t, errors.Is(err, err1))
3434
assert.True(t, errors.Is(err, err2))

pulsar/producer_partition.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,25 +514,25 @@ func (p *partitionProducer) reconnectToBroker(connectionClosed *connectionClosed
514514
if strings.Contains(errMsg, errMsgTopicNotFound) {
515515
// when topic is deleted, we should give up reconnection.
516516
p.log.Warn("Topic not found, stop reconnecting, close the producer")
517-
p.doClose(joinErrors(ErrTopicNotfound, err))
517+
p.doClose(errors.Join(ErrTopicNotfound, err))
518518
return struct{}{}, nil
519519
}
520520

521521
if strings.Contains(errMsg, errMsgTopicTerminated) {
522522
p.log.Warn("Topic was terminated, failing pending messages, stop reconnecting, close the producer")
523-
p.doClose(joinErrors(ErrTopicTerminated, err))
523+
p.doClose(errors.Join(ErrTopicTerminated, err))
524524
return struct{}{}, nil
525525
}
526526

527527
if strings.Contains(errMsg, errMsgProducerBlockedQuotaExceededException) {
528528
p.log.Warn("Producer was blocked by quota exceed exception, failing pending messages, stop reconnecting")
529-
p.failPendingMessages(joinErrors(ErrProducerBlockedQuotaExceeded, err))
529+
p.failPendingMessages(errors.Join(ErrProducerBlockedQuotaExceeded, err))
530530
return struct{}{}, nil
531531
}
532532

533533
if strings.Contains(errMsg, errMsgProducerFenced) {
534534
p.log.Warn("Producer was fenced, failing pending messages, stop reconnecting")
535-
p.doClose(joinErrors(ErrProducerFenced, err))
535+
p.doClose(errors.Join(ErrProducerFenced, err))
536536
return struct{}{}, nil
537537
}
538538

@@ -1111,18 +1111,18 @@ func (p *partitionProducer) SendAsync(ctx context.Context, msg *ProducerMessage,
11111111

11121112
func (p *partitionProducer) validateMsg(msg *ProducerMessage) error {
11131113
if msg == nil {
1114-
return joinErrors(ErrInvalidMessage, fmt.Errorf("message is nil"))
1114+
return errors.Join(ErrInvalidMessage, fmt.Errorf("message is nil"))
11151115
}
11161116

11171117
if msg.Value != nil && msg.Payload != nil {
1118-
return joinErrors(ErrInvalidMessage, fmt.Errorf("can not set Value and Payload both"))
1118+
return errors.Join(ErrInvalidMessage, fmt.Errorf("can not set Value and Payload both"))
11191119
}
11201120

11211121
if p.options.DisableMultiSchema {
11221122
if msg.Schema != nil && p.options.Schema != nil &&
11231123
msg.Schema.GetSchemaInfo().hash() != p.options.Schema.GetSchemaInfo().hash() {
11241124
p.log.Errorf("The producer %s of the topic %s is disabled the `MultiSchema`", p.producerName, p.topic)
1125-
return joinErrors(ErrSchema, fmt.Errorf("msg schema can not match with producer schema"))
1125+
return errors.Join(ErrSchema, fmt.Errorf("msg schema can not match with producer schema"))
11261126
}
11271127
}
11281128

@@ -1138,16 +1138,16 @@ func (p *partitionProducer) prepareTransaction(sr *sendRequest) error {
11381138
if txn.state.Load() != int32(TxnOpen) {
11391139
p.log.WithField("state", txn.state.Load()).Error("Failed to send message" +
11401140
" by a non-open transaction.")
1141-
return joinErrors(ErrTransaction,
1141+
return errors.Join(ErrTransaction,
11421142
fmt.Errorf("failed to send message by a non-open transaction"))
11431143
}
11441144

11451145
if err := txn.registerProducerTopic(p.topic); err != nil {
1146-
return joinErrors(ErrTransaction, err)
1146+
return errors.Join(ErrTransaction, err)
11471147
}
11481148

11491149
if err := txn.registerSendOrAckOp(); err != nil {
1150-
return joinErrors(ErrTransaction, err)
1150+
return errors.Join(ErrTransaction, err)
11511151
}
11521152

11531153
sr.transaction = txn
@@ -1173,7 +1173,7 @@ func (p *partitionProducer) updateSchema(sr *sendRequest) error {
11731173
if schemaVersion == nil {
11741174
schemaVersion, err = p.getOrCreateSchema(schema.GetSchemaInfo())
11751175
if err != nil {
1176-
return joinErrors(ErrSchema, fmt.Errorf("get schema version fail, err: %w", err))
1176+
return errors.Join(ErrSchema, fmt.Errorf("get schema version fail, err: %w", err))
11771177
}
11781178
p.schemaCache.Put(schema.GetSchemaInfo(), schemaVersion)
11791179
}
@@ -1190,15 +1190,15 @@ func (p *partitionProducer) updateUncompressedPayload(sr *sendRequest) error {
11901190
if sr.msg.Value != nil {
11911191
if sr.schema == nil {
11921192
p.log.Errorf("Schema encode message failed %s", sr.msg.Value)
1193-
return joinErrors(ErrSchema, fmt.Errorf("set schema value without setting schema"))
1193+
return errors.Join(ErrSchema, fmt.Errorf("set schema value without setting schema"))
11941194
}
11951195

11961196
// payload and schema are mutually exclusive
11971197
// try to get payload from schema value only if payload is not set
11981198
schemaPayload, err := sr.schema.Encode(sr.msg.Value)
11991199
if err != nil {
12001200
p.log.WithError(err).Errorf("Schema encode message failed %s", sr.msg.Value)
1201-
return joinErrors(ErrSchema, err)
1201+
return errors.Join(ErrSchema, err)
12021202
}
12031203

12041204
sr.uncompressedPayload = schemaPayload

0 commit comments

Comments
 (0)