1
0
Fork 0

Handle errors instead of interfaces

This commit is contained in:
Gregory Eremin 2016-07-27 00:05:22 +02:00
parent a5766e57f8
commit 5e2ce03913
2 changed files with 13 additions and 4 deletions

View File

@ -55,7 +55,7 @@ type BaseDaemon struct {
} }
// PanicHandler is a function that handles panics. Duh! // PanicHandler is a function that handles panics. Duh!
type PanicHandler func(interface{}) type PanicHandler func(error)
// Process creates a task and then adds it to processing queue. // Process creates a task and then adds it to processing queue.
func (d *BaseDaemon) Process(a Actor) { func (d *BaseDaemon) Process(a Actor) {
@ -166,7 +166,7 @@ func (d *BaseDaemon) tryEnqueue(t *task) {
d.queue <- t d.queue <- t
} }
func (d *BaseDaemon) handlePanic(err interface{}) { func (d *BaseDaemon) handlePanic(err error) {
if d.panicHandler != nil { if d.panicHandler != nil {
d.panicHandler(err) d.panicHandler(err)
} }

View File

@ -209,10 +209,11 @@ func (s *Shezmu) processSystemTask(t *task) {
func (s *Shezmu) processGeneralTask(t *task) { func (s *Shezmu) processGeneralTask(t *task) {
defer func() { defer func() {
if err := recover(); err != nil { if val := recover(); val != nil {
err := interfaceToError(val)
s.DaemonStats.Error(t.daemon.String()) s.DaemonStats.Error(t.daemon.String())
t.daemon.base().handlePanic(err) t.daemon.base().handlePanic(err)
s.Logger.Printf("Daemon %s recovered from a panic\nError: %v\n", t.daemon, err) s.Logger.Printf("Daemon %s recovered from a panic\nError: %s\n", t.daemon, err.Error())
debug.PrintStack() debug.PrintStack()
} }
}() }()
@ -227,3 +228,11 @@ func (s *Shezmu) processGeneralTask(t *task) {
func (t *task) String() string { func (t *task) String() string {
return fmt.Sprintf("%s[%s]", t.daemon, t.name) return fmt.Sprintf("%s[%s]", t.daemon, t.name)
} }
func interfaceToError(val interface{}) error {
if terr, ok := val.(error); ok {
return terr
}
return fmt.Errorf("%v", val)
}