diff --git a/cmd/bots/discord/discord.go b/cmd/bots/discord/discord.go index 41d19f14..be15ae72 100644 --- a/cmd/bots/discord/discord.go +++ b/cmd/bots/discord/discord.go @@ -6,6 +6,7 @@ import ( "log" "os" "os/signal" + "runtime/debug" "syscall" "time" @@ -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() @@ -87,6 +102,7 @@ func runDiscordBot() error { } defer func() { _ = logger.Sync() // intentionally ignore error + handleRecover(logger) }() if validationErr := cfg.validate(logger); validationErr != nil { diff --git a/cmd/bots/telegram/telegram.go b/cmd/bots/telegram/telegram.go index 9bd4942c..f6868765 100644 --- a/cmd/bots/telegram/telegram.go +++ b/cmd/bots/telegram/telegram.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "runtime/debug" "syscall" "time" @@ -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() @@ -108,6 +123,7 @@ func runTelegramBot() error { } defer func() { _ = logger.Sync() // intentionally ignore error + handleRecover(logger) }() if validationErr := cfg.validate(logger); validationErr != nil { diff --git a/cmd/nodemon/nodemon.go b/cmd/nodemon/nodemon.go index 67ee8524..1a049bcc 100644 --- a/cmd/nodemon/nodemon.go +++ b/cmd/nodemon/nodemon.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/signal" + "runtime/debug" "strings" "syscall" "time" @@ -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() @@ -191,6 +206,7 @@ func run() error { } defer func() { _ = logger.Sync() // intentionally ignore error + handleRecover(logger) }() if validateErr := cfg.validate(logger); validateErr != nil {