Skip to content

Commit 48f770c

Browse files
committed
organize error
1 parent 46299d7 commit 48f770c

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ var (
1717
errTopicNotFound = errors.New("requested topic was not found")
1818
)
1919

20+
type topicNoCreateError string
21+
22+
func (e topicNoCreateError) Error() string {
23+
return fmt.Sprintf("topic '%s' does not exist but the manager is configured with NoCreate, so it will not attempt to create it", string(e))
24+
}
25+
2026
// this regex matches the package name + some hash info, if we're in gomod but not subpackages
2127
// examples which match
2228
// * github.com/lovoo/goka/processor.go

topic_manager.go

Lines changed: 4 additions & 6 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 fmt.Errorf("error checking topic: %v", 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 topicNoCreateError(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

topic_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func TestTM_EnsureStreamExists(t *testing.T) {
269269
)
270270

271271
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")
272+
require.ErrorIs(t, err, topicNoCreateError(topic))
273273
})
274274
t.Run("fail", func(t *testing.T) {
275275
tm, bm, ctrl := createTopicManager(t)

0 commit comments

Comments
 (0)