Skip to content

Commit

Permalink
feat: return KError instead of errors in AlterConfigs and DescribeCon…
Browse files Browse the repository at this point in the history
…figs (#2472)

Signed-off-by: zhuliquan <zlqlovecode@foxmail.com>
Co-authored-by: zhuliquan <zlq164114@sina.com>
  • Loading branch information
zhuliquan and zhuliquan committed Jan 31, 2024
1 parent 5c3ffea commit d382099
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
10 changes: 2 additions & 8 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,11 +700,8 @@ func (ca *clusterAdmin) DescribeConfig(resource ConfigResource) ([]ConfigEntry,

for _, rspResource := range rsp.Resources {
if rspResource.Name == resource.Name {
if rspResource.ErrorMsg != "" {
return nil, errors.New(rspResource.ErrorMsg)
}
if rspResource.ErrorCode != 0 {
return nil, KError(rspResource.ErrorCode)
return nil, &DescribeConfigError{Err: KError(rspResource.ErrorCode), ErrMsg: rspResource.ErrorMsg}
}
for _, cfgEntry := range rspResource.Configs {
entries = append(entries, *cfgEntry)
Expand Down Expand Up @@ -758,11 +755,8 @@ func (ca *clusterAdmin) AlterConfig(resourceType ConfigResourceType, name string

for _, rspResource := range rsp.Resources {
if rspResource.Name == name {
if rspResource.ErrorMsg != "" {
return errors.New(rspResource.ErrorMsg)
}
if rspResource.ErrorCode != 0 {
return KError(rspResource.ErrorCode)
return &AlterConfigError{Err: KError(rspResource.ErrorCode), ErrMsg: rspResource.ErrorMsg}
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion alter_configs_response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sarama

import "time"
import (
"fmt"
"time"
)

// AlterConfigsResponse is a response type for alter config
type AlterConfigsResponse struct {
Expand All @@ -9,6 +12,19 @@ type AlterConfigsResponse struct {
Resources []*AlterConfigsResourceResponse
}

type AlterConfigError struct {
Err KError
ErrMsg string
}

func (c *AlterConfigError) Error() string {
text := c.Err.Error()
if c.ErrMsg != "" {
text = fmt.Sprintf("%s - %s", text, c.ErrMsg)
}
return text
}

// AlterConfigsResourceResponse is a response type for alter config resource
type AlterConfigsResourceResponse struct {
ErrorCode int16
Expand Down
13 changes: 13 additions & 0 deletions describe_configs_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ const (
SourceDefault
)

type DescribeConfigError struct {
Err KError
ErrMsg string
}

func (c *DescribeConfigError) Error() string {
text := c.Err.Error()
if c.ErrMsg != "" {
text = fmt.Sprintf("%s - %s", text, c.ErrMsg)
}
return text
}

type DescribeConfigsResponse struct {
Version int16
ThrottleTime time.Duration
Expand Down

0 comments on commit d382099

Please sign in to comment.