error handling

This commit is contained in:
pocke 2015-01-12 14:51:59 +09:00
parent eb44ca5fe2
commit cdbd2a79f8
2 changed files with 25 additions and 7 deletions

21
errors.go Normal file
View File

@ -0,0 +1,21 @@
package goevent
import "fmt"
type EventNotDefined struct {
eventName string
}
func newEventNotDefined(name string) *EventNotDefined {
return &EventNotDefined{
eventName: name,
}
}
func (e *EventNotDefined) Error() string {
return fmt.Sprintf("%s event has not been defined yet.", e.eventName)
}
func (e *EventNotDefined) EventName() string {
return e.eventName
}

View File

@ -1,9 +1,6 @@
package goevent
import (
"fmt"
"sync"
)
import "sync"
type Table interface {
On(string, interface{}) error
@ -29,7 +26,7 @@ func (t *table) Trigger(name string, args ...interface{}) error {
ev, ok := t.events[name]
if !ok {
return fmt.Errorf("%s event has not been defined yet.", name)
return newEventNotDefined(name)
}
return ev.Trigger(args...)
@ -53,7 +50,7 @@ func (t *table) Off(name string, f interface{}) error {
e, ok := t.events[name]
if !ok {
return fmt.Errorf("%s event has not been defined yet.", name)
return newEventNotDefined(name)
}
return e.Off(f)
@ -61,7 +58,7 @@ func (t *table) Off(name string, f interface{}) error {
func (t *table) Destroy(name string) error {
if _, ok := t.events[name]; !ok {
return fmt.Errorf("%s event has not been defined yet.", name)
return newEventNotDefined(name)
}
delete(t.events, name)
return nil