From c15cafc1d7593fe4078dbe8da7617923bfa4351c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wysoki=C5=84ski?= Date: Sun, 22 Jan 2023 09:29:20 +0100 Subject: [PATCH] refactor: add missing defers and handle error returned by bot.Run gracefully --- cmd/dcbot/internal/run/run.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/dcbot/internal/run/run.go b/cmd/dcbot/internal/run/run.go index 7e5d3b6..c851a58 100644 --- a/cmd/dcbot/internal/run/run.go +++ b/cmd/dcbot/internal/run/run.go @@ -37,6 +37,9 @@ func New() *cli.Command { if err != nil { return fmt.Errorf("internal.NewBunDB: %w", err) } + defer func() { + _ = db.Close() + }() client, err := internal.NewTWHelpClient(c.App.Version) if err != nil { @@ -67,7 +70,12 @@ func New() *cli.Command { } go func() { - _ = bot.Run() + if runErr := bot.Run(); runErr != nil { + logger.Fatal("bot.Run: " + runErr.Error()) + } + }() + defer func() { + _ = bot.Close() }() if err = os.WriteFile(healthyFilePath, []byte("healthy"), healthyFilePerm); err != nil { @@ -78,10 +86,6 @@ func New() *cli.Command { waitForSignal(c.Context) - if err = bot.Close(); err != nil { - return fmt.Errorf("bot.Close: %w", err) - } - return nil }, }