cli: refactor internal/fsm to use internal/errors

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6694
GitOrigin-RevId: 96938d65f8de857c87e37b7d0fc508cee6436340
This commit is contained in:
Mohd Bilal 2022-11-10 20:27:29 +05:30 committed by hasura-bot
parent e084dea9f8
commit 4c24e6d56b

View File

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/pkg/errors" "github.com/hasura/graphql-engine/cli/v2/internal/errors"
) )
var ErrEventRejected = errors.New("event rejected") var ErrEventRejected = fmt.Errorf("event rejected")
const ( const (
Default StateType = "" Default StateType = ""
@ -39,6 +39,7 @@ type StateMachine struct {
} }
func (s *StateMachine) getNextState(event EventType) (StateType, error) { func (s *StateMachine) getNextState(event EventType) (StateType, error) {
var op errors.Op = "fsm.StateMachine.getNextState"
if state, ok := s.States[s.Current]; ok { if state, ok := s.States[s.Current]; ok {
if state.Events != nil { if state.Events != nil {
if next, ok := state.Events[event]; ok { if next, ok := state.Events[event]; ok {
@ -47,20 +48,21 @@ func (s *StateMachine) getNextState(event EventType) (StateType, error) {
} }
} }
return Default, fmt.Errorf("next state: %w: %s", ErrEventRejected, event) return Default, errors.E(op, fmt.Errorf("next state: %w: %s", ErrEventRejected, event))
} }
func (s *StateMachine) SendEvent(event EventType, eventCtx EventContext) error { func (s *StateMachine) SendEvent(event EventType, eventCtx EventContext) error {
var op errors.Op = "fsm.StateMachine.SendEvent"
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
for { for {
nextState, err := s.getNextState(event) nextState, err := s.getNextState(event)
if err != nil { if err != nil {
return fmt.Errorf("%w: %s", err, event) return errors.E(op, fmt.Errorf("%w: %s", err, event))
} }
state, ok := s.States[nextState] state, ok := s.States[nextState]
if !ok || state.Action == nil { if !ok || state.Action == nil {
return fmt.Errorf("config error") return errors.E(op, fmt.Errorf("config error"))
} }
s.Previous = s.Current s.Previous = s.Current
s.Current = nextState s.Current = nextState