From d382099e8012ee7f271e5d2c0d4a81b41c841c83 Mon Sep 17 00:00:00 2001 From: zhuliquan Date: Thu, 1 Feb 2024 05:08:20 +0800 Subject: [PATCH] feat: return KError instead of errors in AlterConfigs and DescribeConfigs (#2472) Signed-off-by: zhuliquan Co-authored-by: zhuliquan --- admin.go | 10 ++-------- alter_configs_response.go | 18 +++++++++++++++++- describe_configs_response.go | 13 +++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/admin.go b/admin.go index 0ddd031cf..dcf1d7659 100644 --- a/admin.go +++ b/admin.go @@ -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) @@ -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} } } } diff --git a/alter_configs_response.go b/alter_configs_response.go index 658f32e9a..d8b70e371 100644 --- a/alter_configs_response.go +++ b/alter_configs_response.go @@ -1,6 +1,9 @@ package sarama -import "time" +import ( + "fmt" + "time" +) // AlterConfigsResponse is a response type for alter config type AlterConfigsResponse struct { @@ -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 diff --git a/describe_configs_response.go b/describe_configs_response.go index 8aed5de85..386a56885 100644 --- a/describe_configs_response.go +++ b/describe_configs_response.go @@ -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