Skip to content

Commit

Permalink
Add 'recover' call for panics handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Jan 3, 2024
1 parent e479b12 commit 7a5ae6e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/bots/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"

Expand Down Expand Up @@ -76,6 +77,20 @@ func (c *discordBotConfig) validate(zap *zap.Logger) error {
return nil
}

func handleRecover(logger *zap.Logger) {
if r := recover(); r != nil {
if rErr, ok := r.(error); ok {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Error(rErr),
)
} else {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Any("recovered-data", r),
)
}
}
}

func runDiscordBot() error {
cfg := newDiscordBotConfigConfig()
flag.Parse()
Expand All @@ -87,6 +102,7 @@ func runDiscordBot() error {
}
defer func() {
_ = logger.Sync() // intentionally ignore error
handleRecover(logger)
}()

if validationErr := cfg.validate(logger); validationErr != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/bots/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"

Expand Down Expand Up @@ -97,6 +98,20 @@ func (c *telegramBotConfig) validate(logger *zap.Logger) error {
return nil
}

func handleRecover(logger *zap.Logger) {
if r := recover(); r != nil {
if rErr, ok := r.(error); ok {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Error(rErr),
)
} else {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Any("recovered-data", r),
)
}
}
}

func runTelegramBot() error {
cfg := newTelegramBotConfig()
flag.Parse()
Expand All @@ -108,6 +123,7 @@ func runTelegramBot() error {
}
defer func() {
_ = logger.Sync() // intentionally ignore error
handleRecover(logger)
}()

if validationErr := cfg.validate(logger); validationErr != nil {
Expand Down
16 changes: 16 additions & 0 deletions cmd/nodemon/nodemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/signal"
"runtime/debug"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -180,6 +181,20 @@ func (c *nodemonConfig) runDiscordPairServer() bool { return c.nanomsgPairDiscor

func (c *nodemonConfig) runTelegramPairServer() bool { return c.nanomsgPairTelegramURL != "" }

func handleRecover(logger *zap.Logger) {
if r := recover(); r != nil {
if rErr, ok := r.(error); ok {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Error(rErr),
)
} else {
logger.Fatal("Panic has been occurred", zap.ByteString("stacktrace", debug.Stack()),
zap.Any("recovered-data", r),
)
}
}
}

func run() error {
cfg := newNodemonConfig()
flag.Parse()
Expand All @@ -191,6 +206,7 @@ func run() error {
}
defer func() {
_ = logger.Sync() // intentionally ignore error
handleRecover(logger)
}()

if validateErr := cfg.validate(logger); validateErr != nil {
Expand Down

0 comments on commit 7a5ae6e

Please sign in to comment.