implement DefaultRecoverHandler

This commit is contained in:
Liam Stanley 2017-02-13 01:27:57 -05:00
parent 88057bed20
commit d8423fc605
2 changed files with 23 additions and 6 deletions

@ -112,7 +112,10 @@ type Config struct {
// not set, the client will panic. identifier is generally going to be the
// callback ID. The file and line should point to the exact item that
// threw a panic, and stack is the full stack trace of how RecoverFunc
// caught it.
// caught it. Set this to DefaultRecoverHandler if you don't want the
// client to panic, however you don't want to handle the panic yourself.
// DefaultRecoverHandler will log the panic to Debugger or os.Stdout if
// Debugger is unset.
RecoverFunc func(c *Client, e *HandlerError)
// SupportedCaps are the IRCv3 capabilities you would like the client to
// support. Only use this if DisableTracking and DisableCapTracking are
@ -173,10 +176,11 @@ func New(config Config) *Client {
}
if c.Config.Debugger == nil {
c.Config.Debugger = ioutil.Discard
c.debug = log.New(ioutil.Discard, "", 0)
} else {
c.debug = log.New(c.Config.Debugger, "debug:", log.Ltime|log.Lshortfile)
c.debug.Print("initializing debugging")
}
c.debug = log.New(c.Config.Debugger, "debug:", log.Ltime|log.Lshortfile)
c.debug.Print("initializing debugging")
// Setup the caller.
c.Handlers = newCaller(c.debug)

@ -366,8 +366,6 @@ func recoverHandlerPanic(client *Client, event *Event, id string, skip int) {
callOk: ok,
}
client.debug.Println(err.Error())
client.debug.Println(err.String())
client.Config.RecoverFunc(client, err)
return
}
@ -401,3 +399,18 @@ func (e *HandlerError) Error() string {
func (e *HandlerError) String() string {
return fmt.Sprintf("panic: %s\n\n%s", e.Panic, string(e.Stack))
}
// DefaultRecoverHandler can be used with Config.RecoverFunc as a default
// catch-all for panics. This will log the error, and the call trace to
// the debug log (see Config.Debugger), or os.Stdout if Config.Debugger is
// unset.
func DefaultRecoverHandler(client *Client, err *HandlerError) {
if client.Config.Debugger == nil {
fmt.Println(err.Error())
fmt.Println(err.String())
return
}
client.debug.Println(err.Error())
client.debug.Println(err.String())
}