Skip to content

Instantly share code, notes, and snippets.

@bench
Created June 26, 2017 13:22
Show Gist options
  • Save bench/58ad92ebb0e3e7c25721712311703b18 to your computer and use it in GitHub Desktop.
Save bench/58ad92ebb0e3e7c25721712311703b18 to your computer and use it in GitHub Desktop.
type PanicFilter struct {
mq.Handler
logger *log.Logger
}
func (f PanicFilter) Handle(d amqp.Delivery) error {
defer func() {
if panic := recover(); panic != nil {
defer d.Nack(false, false) // might have been ACK/NACK already, but for safety.
_, file, line, ok := runtime.Caller(4)
if !ok {
return
}
stack := make([]byte, 4096)
stack = stack[:runtime.Stack(stack, false)]
f.logger.Printf("Trapped panic [%s] while handling delivery (%s, line %d)\nStack:\n%s", panic, file, line, string(stack))
}
}()
return f.Handler.Handle(d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment