named error types

main
Nick Dumas 2 years ago
parent bc70a6ca28
commit cd9d4829a8

@ -2,9 +2,13 @@ package fsm
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
) )
var NoMoreTransitions = errors.New("no more transitions")
var InvalidEvent = errors.New("invalid event")
type State string type State string
type Event string type Event string
@ -32,12 +36,12 @@ func (m *StateMachine) getCurrentNode() *Node {
func (m *StateMachine) getNextNode(event Event) (*Node, error) { func (m *StateMachine) getNextNode(event Event) (*Node, error) {
if m.CurrentNode == nil { if m.CurrentNode == nil {
return nil, fmt.Errorf("nowhere to go anymore!\n") return nil, NoMoreTransitions
} }
transition, ok := m.CurrentNode.Transitions[event] transition, ok := m.CurrentNode.Transitions[event]
if !ok { if !ok {
return nil, fmt.Errorf("invalid event: %v", event) return nil, InvalidEvent
} }
return transition.Node, nil return transition.Node, nil

Loading…
Cancel
Save