Handle errors instead of interfaces
This commit is contained in:
parent
a5766e57f8
commit
5e2ce03913
|
@ -55,7 +55,7 @@ type BaseDaemon struct {
|
|||
}
|
||||
|
||||
// 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.
|
||||
func (d *BaseDaemon) Process(a Actor) {
|
||||
|
@ -166,7 +166,7 @@ func (d *BaseDaemon) tryEnqueue(t *task) {
|
|||
d.queue <- t
|
||||
}
|
||||
|
||||
func (d *BaseDaemon) handlePanic(err interface{}) {
|
||||
func (d *BaseDaemon) handlePanic(err error) {
|
||||
if d.panicHandler != nil {
|
||||
d.panicHandler(err)
|
||||
}
|
||||
|
|
13
shezmu.go
13
shezmu.go
|
@ -209,10 +209,11 @@ func (s *Shezmu) processSystemTask(t *task) {
|
|||
|
||||
func (s *Shezmu) processGeneralTask(t *task) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
if val := recover(); val != nil {
|
||||
err := interfaceToError(val)
|
||||
s.DaemonStats.Error(t.daemon.String())
|
||||
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()
|
||||
}
|
||||
}()
|
||||
|
@ -227,3 +228,11 @@ func (s *Shezmu) processGeneralTask(t *task) {
|
|||
func (t *task) String() string {
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue