3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-01-21 09:44:21 +01:00
ergo/irc/panic.go

25 lines
653 B
Go
Raw Normal View History

2021-09-19 03:28:16 +02:00
// Copyright (c) 2021 Shivaram Lingamneni
// released under the MIT license
package irc
import (
"fmt"
"runtime/debug"
2025-01-14 03:47:21 +01:00
"time"
2021-09-19 03:28:16 +02:00
)
// HandlePanic is a general-purpose panic handler for ad-hoc goroutines.
// Because of the semantics of `recover`, it must be called directly
// from the routine on whose call stack the panic would occur, with `defer`,
// e.g. `defer server.HandlePanic()`
2025-01-14 03:47:21 +01:00
func (server *Server) HandlePanic(restartable func()) {
2021-09-19 03:28:16 +02:00
if r := recover(); r != nil {
server.logger.Error("internal", fmt.Sprintf("Panic encountered: %v\n%s", r, debug.Stack()))
2025-01-14 03:47:21 +01:00
if restartable != nil {
time.Sleep(time.Second)
go restartable()
}
2021-09-19 03:28:16 +02:00
}
}