From: Jeff Halter Date: Sun, 3 Jul 2022 22:09:20 +0000 (-0700) Subject: Move panic handler X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/a9bdccb79164a787b4f1a2c1579a95cf751aef40?hp=958108952eec4cef92bcd26cd0c845aaed5a4982 Move panic handler --- diff --git a/hotline/panic.go b/hotline/panic.go new file mode 100644 index 0000000..e7c97db --- /dev/null +++ b/hotline/panic.go @@ -0,0 +1,15 @@ +package hotline + +import ( + "fmt" + "go.uber.org/zap" + "runtime/debug" +) + +// dontPanic logs panics instead of crashing +func dontPanic(logger *zap.SugaredLogger) { + if r := recover(); r != nil { + fmt.Println("stacktrace from panic: \n" + string(debug.Stack())) + logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack())) + } +} diff --git a/hotline/server.go b/hotline/server.go index 1fafb30..ccf992e 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -18,7 +18,6 @@ import ( "net" "os" "path/filepath" - "runtime/debug" "strings" "sync" "time" @@ -542,14 +541,6 @@ func (s *Server) loadConfig(path string) error { return nil } -// dontPanic logs panics instead of crashing -func dontPanic(logger *zap.SugaredLogger) { - if r := recover(); r != nil { - fmt.Println("stacktrace from panic: \n" + string(debug.Stack())) - logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack())) - } -} - // handleNewConnection takes a new net.Conn and performs the initial login sequence func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error { defer dontPanic(s.Logger)