From 58bf74ad2a1a8a2b1748473c70565476f1a9adbd Mon Sep 17 00:00:00 2001 From: Travis Bischel Date: Thu, 7 Oct 2021 13:07:26 -0600 Subject: [PATCH] client: complement prior commit with better error message Now that we collapse missing topics / partitions / leaders into one error, we can make a nicer error message that indicates exactly how many things missing there were in the error. This avoids needing to inspect the request to build a similar error string. --- pkg/kgo/client.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/kgo/client.go b/pkg/kgo/client.go index 043e0efe..d3927922 100644 --- a/pkg/kgo/client.go +++ b/pkg/kgo/client.go @@ -1631,6 +1631,7 @@ func (cl *Client) fetchMappedMetadata(ctx context.Context, topics []string) (map return mapping, nil } +// These errors pair with "collect" below for wording. var ( errMissingTopic = errors.New("topic was not returned when looking up its metadata") errMissingPartition = errors.New("partition was not returned when looking up its metadata") @@ -1721,10 +1722,34 @@ func (l *unknownErrShards) collect(mkreq, mergeParts interface{}) []issueShard { req := factory.Call(nil)[0] + var ntopics, npartitions int for topic, partitions := range topics { + ntopics++ + npartitions += partitions.Len() perTopic.Call([]reflect.Value{req, reflect.ValueOf(topic), partitions}) } + switch err { + case errMissingTopic: + if ntopics == 1 { + err = errors.New("1 topic was not returned when lookup up its metadata") + } else if ntopics > 1 { + err = fmt.Errorf("%d topics were not returned when lookup up their metadata", ntopics) + } + case errMissingPartition: + if npartitions == 1 { + err = errors.New("1 partition was not returned when looking up its metadata") + } else if npartitions > 1 { + err = fmt.Errorf("%d partitions were not returned when looking up their metadata", npartitions) + } + case errNoLeader: + if npartitions == 1 { + err = errors.New("1 partition has no leader from a metadata lookup") + } else if npartitions > 1 { + err = fmt.Errorf("%d partitions have no leader from a metadata lookup", npartitions) + } + } + shards = append(shards, issueShard{ req: req.Interface().(kmsg.Request), err: err,