From 4abd88f40b6b5cdbd46113c5645906fdb6633576 Mon Sep 17 00:00:00 2001 From: raylu Date: Wed, 27 Apr 2011 04:37:55 -0400 Subject: [PATCH] Add recovery in handlePrivmsg For an explanation of the voodoo, see https://github.com/orfjackal/gospec/blob/master/src/gospec/recover.go#L44 https://github.com/orfjackal/gospec/blob/release/src/gospec/funcname.go#L22 https://github.com/orfjackal/gospec/blob/master/src/gospec/location.go#L43 Also note that in the next release of Go (or weekly.2011-04-13), cutoff can be written using the code at https://github.com/orfjackal/gospec/blob/master/src/gospec/funcname.go#L22 --- handler.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/handler.go b/handler.go index 7ffcb48..fad19f3 100644 --- a/handler.go +++ b/handler.go @@ -4,6 +4,8 @@ import ( irc "github.com/fluffle/goirc/client" "fmt" "strings" + "runtime" + "reflect" "http" "xml" "strconv" @@ -59,6 +61,23 @@ func handlePrivmsg(conn *irc.Conn, line *irc.Line) { return } + defer func() { + if r := recover(); r != nil { + fmt.Println("Recovered from", r) + callers := make([]uintptr, 10) + runtime.Callers(4, callers) + cutoff := runtime.FuncForPC(reflect.NewValue(handlePrivmsg).(*reflect.FuncValue).Get()).Entry() + for _, pc := range callers { + function := runtime.FuncForPC(pc - 1) + if function.Entry() == cutoff{ + break + } + file, line := function.FileLine(pc - 1) + fmt.Printf("%s:%d\n\t%s\n", file, line, function.Name()) + } + } + }() + target := line.Args[0] if isChannel(target) { // message to a channel