Skip to content

Commit bb8d98b

Browse files
committed
organize error
1 parent 46299d7 commit bb8d98b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ var (
1515
errBuildProducer = "error creating Kafka producer: %v"
1616
errApplyOptions = "error applying options: %v"
1717
errTopicNotFound = errors.New("requested topic was not found")
18+
errTopicNoCreate = func(topic string) error {
19+
return fmt.Errorf("topic '%s' does not exist but the manager is configured with NoCreate, so it will not attempt to create it", topic)
20+
}
21+
errTopicChecking = func(topic string, err error) error {
22+
return fmt.Errorf("error checking topic '%s': %w", topic, err)
23+
}
1824
)
1925

2026
// this regex matches the package name + some hash info, if we're in gomod but not subpackages

topic_manager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,15 @@ func (m *topicManager) ensureExists(topic string, npar, rfactor int, config map[
172172

173173
partitions, err := m.Partitions(topic)
174174

175-
if err != nil {
176-
if err != errTopicNotFound {
177-
return fmt.Errorf("error checking topic: %v", err)
178-
}
175+
if err != nil && err != errTopicNotFound {
176+
return errTopicChecking(topic, err)
179177
}
180178
// no topic yet, let's create it
181179
if len(partitions) == 0 {
182180

183181
// (or not)
184182
if m.topicManagerConfig.NoCreate {
185-
return fmt.Errorf("topic `%s` does not exist but the manager is configured with NoCreate, so it will not attempt to create it", topic)
183+
return errTopicNoCreate(topic)
186184
}
187185

188186
return m.createTopic(topic,
@@ -195,7 +193,7 @@ func (m *topicManager) ensureExists(topic string, npar, rfactor int, config map[
195193

196194
// partitions do not match
197195
if len(partitions) != npar {
198-
return m.handleConfigMismatch(fmt.Sprintf("partition count mismatch for topic %s. Need %d, but existing topic has %d", topic, npar, len(partitions)))
196+
return m.handleConfigMismatch(fmt.Sprintf("partition count mismatch for topic '%s'. Need %d, but existing topic has %d", topic, npar, len(partitions)))
199197
}
200198

201199
// check additional config values via the cluster admin if our current version supports it
@@ -249,7 +247,7 @@ func (m *topicManager) waitForCreated(topic string) error {
249247
case errTopicNotFound:
250248
time.Sleep(time.Second)
251249
default:
252-
return fmt.Errorf("error checking topic: %w", err)
250+
return errTopicChecking(topic, err)
253251
}
254252
}
255253
return fmt.Errorf("waiting for topic %s to be created timed out", topic)

topic_manager_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"go.uber.org/mock/gomock"
1111
)
1212

13-
var tmTestBrokers = []string{"0"}
14-
1513
func trueCheckFunc(broker Broker, config *sarama.Config) error {
1614
return nil
1715
}
@@ -269,7 +267,7 @@ func TestTM_EnsureStreamExists(t *testing.T) {
269267
)
270268

271269
err := tm.EnsureStreamExists(topic, npar)
272-
require.Equal(t, err.Error(), "topic `some-topic` does not exist but the manager is configured with NoCreate, so it will not attempt to create it")
270+
require.Equal(t, err, errTopicNoCreate(topic))
273271
})
274272
t.Run("fail", func(t *testing.T) {
275273
tm, bm, ctrl := createTopicManager(t)

0 commit comments

Comments
 (0)