Skip to content

Commit

Permalink
kadm: return defined errors for On functions
Browse files Browse the repository at this point in the history
kerr errors are defined enough and can be better used with errors.As and
errors.Is.
  • Loading branch information
twmb committed Dec 2, 2021
1 parent 90557f2 commit 6ab9044
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 27 deletions.
8 changes: 4 additions & 4 deletions pkg/kadm/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ResourceConfigs []ResourceConfig
// The fn is given a copy of the config. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the resource does not exist, this returns an error indicating it is missing.
// If the resource does not exist, this returns kerr.UnknownTopicOrPartition.
func (rs ResourceConfigs) On(name string, fn func(*ResourceConfig) error) (ResourceConfig, error) {
for _, r := range rs {
if r.Name == name {
Expand All @@ -66,7 +66,7 @@ func (rs ResourceConfigs) On(name string, fn func(*ResourceConfig) error) (Resou
return r, fn(&r)
}
}
return ResourceConfig{}, errMissing(name)
return ResourceConfig{}, kerr.UnknownTopicOrPartition
}

// DescribeTopicConfigs returns the configuration for the requested topics.
Expand Down Expand Up @@ -196,7 +196,7 @@ type AlterConfigsResponses []AlterConfigsResponse
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the resource does not exist, this returns an error indicating it is missing.
// If the resource does not exist, this returns kerr.UnknownTopicOrPartition.
func (rs AlterConfigsResponses) On(name string, fn func(*AlterConfigsResponse) error) (AlterConfigsResponse, error) {
for _, r := range rs {
if r.Name == name {
Expand All @@ -206,7 +206,7 @@ func (rs AlterConfigsResponses) On(name string, fn func(*AlterConfigsResponse) e
return r, fn(&r)
}
}
return AlterConfigsResponse{}, errMissing(name)
return AlterConfigsResponse{}, kerr.UnknownTopicOrPartition
}

// AlterTopicConfigs incrementally alters topic configuration values.
Expand Down
8 changes: 0 additions & 8 deletions pkg/kadm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ import (
"github.com/twmb/franz-go/pkg/kmsg"
)

func errMissing(name string) error {
return fmt.Errorf("missing %q", name)
}

func errPartMissing(t string, p int32) error {
return fmt.Errorf("missing %q partition %d", t, p)
}

// AuthError can be returned from requests for resources that you are not
// authorized for.
type AuthError struct {
Expand Down
12 changes: 6 additions & 6 deletions pkg/kadm/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (ds DescribedGroups) Sorted() []DescribedGroup {
// Modifications on a described group's inner fields are persisted to the
// original map (because slices are pointers).
//
// If the group does not exist, this returns an error indicating it is missing.
// If the group does not exist, this returns kerr.GroupIDNotFound.
func (rs DescribedGroups) On(group string, fn func(*DescribedGroup) error) (DescribedGroup, error) {
if len(rs) > 0 {
r, ok := rs[group]
Expand All @@ -161,7 +161,7 @@ func (rs DescribedGroups) On(group string, fn func(*DescribedGroup) error) (Desc
return r, fn(&r)
}
}
return DescribedGroup{}, errMissing(group)
return DescribedGroup{}, kerr.GroupIDNotFound
}

// Topics returns a sorted list of all group names.
Expand Down Expand Up @@ -373,7 +373,7 @@ func (ds DeleteGroupResponses) Sorted() []DeleteGroupResponse {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the group does not exist, this returns an error indicating it is missing.
// If the group does not exist, this returns kerr.GroupIDNotFound.
func (rs DeleteGroupResponses) On(group string, fn func(*DeleteGroupResponse) error) (DeleteGroupResponse, error) {
if len(rs) > 0 {
r, ok := rs[group]
Expand All @@ -384,7 +384,7 @@ func (rs DeleteGroupResponses) On(group string, fn func(*DeleteGroupResponse) er
return r, fn(&r)
}
}
return DeleteGroupResponse{}, errMissing(group)
return DeleteGroupResponse{}, kerr.GroupIDNotFound
}

// DeleteGroups deletes all groups specified.
Expand Down Expand Up @@ -750,7 +750,7 @@ func (rs FetchOffsetsResponses) AllFailed() bool {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the group does not exist, this returns an error indicating it is missing.
// If the group does not exist, this returns kerr.GroupIDNotFound.
func (rs FetchOffsetsResponses) On(group string, fn func(*FetchOffsetsResponse) error) (FetchOffsetsResponse, error) {
if len(rs) > 0 {
r, ok := rs[group]
Expand All @@ -761,7 +761,7 @@ func (rs FetchOffsetsResponses) On(group string, fn func(*FetchOffsetsResponse)
return r, fn(&r)
}
}
return FetchOffsetsResponse{}, errMissing(group)
return FetchOffsetsResponse{}, kerr.GroupIDNotFound
}

// FetchManyOffsets issues a fetch offsets requests for each group specified.
Expand Down
18 changes: 9 additions & 9 deletions pkg/kadm/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (rs CreateTopicResponses) Sorted() []CreateTopicResponse {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the topic does not exist, this returns an error indicating it is missing.
// If the topic does not exist, this returns kerr.UnknownTopicOrPartition.
func (rs CreateTopicResponses) On(topic string, fn func(*CreateTopicResponse) error) (CreateTopicResponse, error) {
if len(rs) > 0 {
r, ok := rs[topic]
Expand All @@ -84,7 +84,7 @@ func (rs CreateTopicResponses) On(topic string, fn func(*CreateTopicResponse) er
return r, fn(&r)
}
}
return CreateTopicResponse{}, errMissing(topic)
return CreateTopicResponse{}, kerr.UnknownTopicOrPartition
}

// CreateTopics issues a create topics request with the given partitions,
Expand Down Expand Up @@ -197,7 +197,7 @@ func (rs DeleteTopicResponses) Sorted() []DeleteTopicResponse {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the topic does not exist, this returns an error indicating it is missing.
// If the topic does not exist, this returns kerr.UnknownTopicOrPartition.
func (rs DeleteTopicResponses) On(topic string, fn func(*DeleteTopicResponse) error) (DeleteTopicResponse, error) {
if len(rs) > 0 {
r, ok := rs[topic]
Expand All @@ -208,7 +208,7 @@ func (rs DeleteTopicResponses) On(topic string, fn func(*DeleteTopicResponse) er
return r, fn(&r)
}
}
return DeleteTopicResponse{}, errMissing(topic)
return DeleteTopicResponse{}, kerr.UnknownTopicOrPartition
}

// DeleteTopics issues a delete topics request for the given topic names with a
Expand Down Expand Up @@ -317,8 +317,8 @@ func (rs DeleteRecordsResponses) Sorted() []DeleteRecordsResponse {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the topic or partition does not exist, this returns an error indicating
// it is missing.
// If the topic or partition does not exist, this returns
// kerr.UnknownTopicOrPartition.
func (rs DeleteRecordsResponses) On(topic string, partition int32, fn func(*DeleteRecordsResponse) error) (DeleteRecordsResponse, error) {
if len(rs) > 0 {
t, ok := rs[topic]
Expand All @@ -332,7 +332,7 @@ func (rs DeleteRecordsResponses) On(topic string, partition int32, fn func(*Dele
}
}
}
return DeleteRecordsResponse{}, errPartMissing(topic, partition)
return DeleteRecordsResponse{}, kerr.UnknownTopicOrPartition
}

// DeleteRecords issues a delete records request for the given offsets. Per
Expand Down Expand Up @@ -411,7 +411,7 @@ func (rs CreatePartitionsResponses) Sorted() []CreatePartitionsResponse {
// The fn is given a copy of the response. This function returns the copy as
// well; any modifications within fn are modifications on the returned copy.
//
// If the topic does not exist, this returns an error indicating it is missing.
// If the topic does not exist, this returns kerr.UnknownTopicOrPartition.
func (rs CreatePartitionsResponses) On(topic string, fn func(*CreatePartitionsResponse) error) (CreatePartitionsResponse, error) {
if len(rs) > 0 {
r, ok := rs[topic]
Expand All @@ -422,7 +422,7 @@ func (rs CreatePartitionsResponses) On(topic string, fn func(*CreatePartitionsRe
return r, fn(&r)
}
}
return CreatePartitionsResponse{}, errMissing(topic)
return CreatePartitionsResponse{}, kerr.UnknownTopicOrPartition
}

// CreatePartitions issues a create partitions request for the given topics,
Expand Down

0 comments on commit 6ab9044

Please sign in to comment.