mirror of https://github.com/pocke/goevent.git
error handling
This commit is contained in:
parent
eb44ca5fe2
commit
cdbd2a79f8
|
@ -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
|
||||
}
|
11
table.go
11
table.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue