Skip to content

Commit

Permalink
client: complement prior commit with better error message
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
twmb committed Oct 7, 2021
1 parent 19f4e9b commit 58bf74a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/kgo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 58bf74a

Please sign in to comment.