Skip to content

Commit

Permalink
fix(topic): only return error on http.StatusOK
Browse files Browse the repository at this point in the history
The topic name validator should only be returning an error
when this topic name already exists.
  • Loading branch information
Enda Phelan committed Aug 31, 2021
1 parent 950a3f5 commit 8f8863b
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions pkg/kafka/topic/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (v *Validator) ValidateMessageRetentionSize(val interface{}) error {

// ValidateNameIsAvailable checks if a topic with the given name already exists
func (v *Validator) ValidateNameIsAvailable(val interface{}) error {
name, _ := val.(string)
name := fmt.Sprintf("%v", val)

conn, err := v.Connection(connection.DefaultConfigRequireMasAuth)
if err != nil {
Expand All @@ -154,15 +154,12 @@ func (v *Validator) ValidateNameIsAvailable(val interface{}) error {
return err
}

_, httpRes, err := api.TopicsApi.GetTopic(context.Background(), name).Execute()
defer httpRes.Body.Close()
if err != nil {
return err
}
_, httpRes, _ := api.TopicsApi.GetTopic(context.Background(), name).Execute()

if httpRes != nil && httpRes.StatusCode == http.StatusOK {
return errors.New(v.Localizer.MustLocalize("kafka.topic.create.error.conflictError", localize.NewEntry("TopicName", name), localize.NewEntry("InstanceName", kafkaInstance.GetName())))
}
defer httpRes.Body.Close()

return nil
}

0 comments on commit 8f8863b

Please sign in to comment.