Skip to content

Commit 3be7293

Browse files
author
Marek Czernek
committed
enhancement: Improve error handling
1 parent fc816bb commit 3be7293

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ build-all:
1919
GOOS=linux GOARCH=$(GOARCH) $(GOBUILD) -o bin/$(BINARY_NAME)_linux -v $(GOPKG)
2020
GOOS=darwin GOARCH=$(GOARCH) $(GOBUILD) -o bin/$(BINARY_NAME)_darwin -v $(GOPKG)
2121

22+
.PHONY: clean
23+
clean:
24+
-rm bin/*
25+
2226
.PHONY: all
2327
all: build

main/main.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,34 @@ var removed int = 0
1818
func main() {
1919
parseCmdFlags()
2020
cfg, err := GetConfig(configPath)
21-
if err != nil {
22-
fmt.Printf("Could not parse configuration yaml: %v\n", err)
23-
os.Exit(1)
24-
}
21+
handleError(err, "[ERR] Could not parse configuration yaml\n", true)
2522

2623
client := getGithubClient(cfg)
2724

2825
var wg sync.WaitGroup
2926

30-
issues, _, _ := client.Issues.ListByRepo(context.Background(), cfg.Repo.Owner, cfg.Repo.Name, nil)
27+
issues, _, err := client.Issues.ListByRepo(context.Background(), cfg.Repo.Owner, cfg.Repo.Name, nil)
28+
handleError(err, "[ERR] Could not connect to GitHub API, check API token?\n", true)
29+
3130
wg.Add(len(issues))
3231

3332
for _, issue := range issues {
3433
go deleteIssueReactions(&wg, client, cfg, *issue.Number)
3534
}
3635
wg.Wait()
37-
fmt.Printf("Successfully removed %d issue reactions from repo %v\n", removed, cfg.Repo)
36+
fmt.Printf("Removed %d issue reactions from repo %v\n", removed, cfg.Repo)
3837
}
3938

4039
func deleteIssueReactions(wg *sync.WaitGroup, client *github.Client, cfg *Config, issueNumber int) {
4140
defer wg.Done()
4241
ctx := context.Background()
43-
reactions, _, _ := client.Reactions.ListIssueReactions(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, nil)
42+
reactions, _, err := client.Reactions.ListIssueReactions(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, nil)
43+
handleError(err, fmt.Sprintf("[WARN] Could not list reactions for issue %d", issueNumber), false)
4444
for _, reaction := range reactions {
4545
if *reaction.User.Login == cfg.Auth.Login {
4646
_, err := client.Reactions.DeleteIssueReaction(ctx, cfg.Repo.Owner, cfg.Repo.Name, issueNumber, *reaction.ID)
4747
if err != nil {
48-
fmt.Println("[ERR] Could not delete reaction ", reaction)
49-
fmt.Println(err)
48+
fmt.Fprintf(os.Stderr, "[WARN] Could not delete reaction %v\nerror:\n%v", reaction, err)
5049
} else {
5150
removed++
5251
}
@@ -72,3 +71,13 @@ func getGithubClient(cfg *Config) *github.Client {
7271
tc := oauth2.NewClient(ctx, ts)
7372
return github.NewClient(tc)
7473
}
74+
75+
func handleError(err error, msg string, fatal bool) {
76+
if err != nil {
77+
fmt.Fprintf(os.Stderr, msg)
78+
fmt.Fprintf(os.Stderr, "%v\n", err)
79+
if fatal {
80+
os.Exit(1)
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)