Skip to content

Commit 9526ae7

Browse files
authored
Manage non topic specific topicctl errors (#32)
* Process errors between topicctl and parser * Add more details to the error msg
1 parent dda1d80 commit 9526ae7

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

cmd/topicctl/subcmd/apply.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"os"
88
"os/signal"
99
"path/filepath"
10+
"strings"
1011
"syscall"
1112
"time"
13+
"unicode"
1214

1315
"github.com/segmentio/topicctl/pkg/admin"
1416
"github.com/segmentio/topicctl/pkg/apply"
@@ -215,6 +217,16 @@ func applyRun(cmd *cobra.Command, args []string) error {
215217
}
216218

217219
// prints changes as JSON to stdout
220+
// sanitizeErrorString replaces all non-printable characters with spaces
221+
func sanitizeErrorString(s string) string {
222+
return strings.Map(func(r rune) rune {
223+
if unicode.IsPrint(r) {
224+
return r
225+
}
226+
return ' '
227+
}, s)
228+
}
229+
218230
func printJson(changes apply.NewOrUpdatedChanges) (map[string]interface{}, error) {
219231
jsonChanges, err := json.Marshal(changes)
220232
if err != nil {
@@ -320,11 +332,16 @@ func applyTopic(
320332
// Some topic creation errors also still create the topic
321333
log.Error("Error detected while creating or updating a topic")
322334
log.Error("The following changes were still made:")
323-
partialChanges, printErr := printJson(topicChanges)
324-
if printErr != nil {
325-
log.Error("Error printing JSON changes data")
335+
336+
if topicChanges == nil {
337+
fmt.Printf("{\"error\": \"%s\"}\n", sanitizeErrorString(err.Error()))
326338
} else {
327-
log.Errorf("%#v", partialChanges)
339+
partialChanges, printErr := printJson(topicChanges)
340+
if printErr != nil {
341+
log.Error("Error printing JSON changes data")
342+
} else {
343+
log.Errorf("%#v", partialChanges)
344+
}
328345
}
329346
return err
330347
}

py/parse_and_notify.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,21 @@ def main():
251251
)
252252

253253
for line in sys.stdin:
254-
topic = json.loads(line)
254+
try:
255+
topic = json.loads(line)
256+
except json.JSONDecodeError as e:
257+
title = f"Topicctl failed to apply in region {SENTRY_REGION}"
258+
slack_notifier.send(
259+
title=title, body=f"Topicctl produced invalid JSON: {e}"
260+
)
261+
raise
262+
263+
if "error" in topic:
264+
title = f"Topicctl failed to apply in region {SENTRY_REGION}"
265+
slack_notifier.send(title=title, body=topic["error"])
266+
print(f"Error: {topic['error']}", file=sys.stderr)
267+
exit(-1)
268+
255269
action = topic["action"]
256270
topic_content = (
257271
NewTopic.build(topic)

0 commit comments

Comments
 (0)