Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add introspection interfaces for some fsm errors #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package fsm

import (
"context"
"errors"
)

// InvalidEventError is returned by FSM.Event() when the event cannot be called
Expand Down Expand Up @@ -69,6 +70,15 @@ func (e NoTransitionError) Error() string {
return "no transition"
}

func (e NoTransitionError) Is(target error) bool {
_, ok := target.(NoTransitionError)
return ok || errors.Is(e.Err, target)
}

func (e NoTransitionError) Unwrap() error {
return e.Err
}

// CanceledError is returned by FSM.Event() when a callback have canceled a
// transition.
type CanceledError struct {
Expand All @@ -82,6 +92,15 @@ func (e CanceledError) Error() string {
return "transition canceled"
}

func (e CanceledError) Is(target error) bool {
_, ok := target.(CanceledError)
return ok || errors.Is(e.Err, target)
}

func (e CanceledError) Unwrap() error {
return e.Err
}

// AsyncError is returned by FSM.Event() when a callback have initiated an
// asynchronous state transition.
type AsyncError struct {
Expand All @@ -98,6 +117,15 @@ func (e AsyncError) Error() string {
return "async started"
}

func (e AsyncError) Is(target error) bool {
_, ok := target.(AsyncError)
return ok || errors.Is(e.Err, target)
}

func (e AsyncError) Unwrap() error {
return e.Err
}

// InternalError is returned by FSM.Event() and should never occur. It is a
// probably because of a bug.
type InternalError struct{}
Expand Down
46 changes: 43 additions & 3 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,71 @@ func TestNotInTransitionError(t *testing.T) {

func TestNoTransitionError(t *testing.T) {
e := NoTransitionError{}
innerErr := errors.New("no transition")
if e.Error() != "no transition" {
t.Error("NoTransitionError string mismatch")
}
e.Err = errors.New("no transition")
e.Err = innerErr
if e.Error() != "no transition with error: "+e.Err.Error() {
t.Error("NoTransitionError string mismatch")
}

realErr := hideErrInterfaceType(e)
if !errors.Is(realErr, NoTransitionError{}) {
t.Error("NoTransitionError 'Is' broken")
}
if !errors.Is(realErr, innerErr) {
t.Error("NoTransitionError 'Is' broken")
}
if errors.Unwrap(e) != innerErr {
t.Error("NoTransitionError 'Unwrap' broken")
}
}

func TestCanceledError(t *testing.T) {
e := CanceledError{}
innerErr := errors.New("canceled")
if e.Error() != "transition canceled" {
t.Error("CanceledError string mismatch")
}
e.Err = errors.New("canceled")
e.Err = innerErr
if e.Error() != "transition canceled with error: "+e.Err.Error() {
t.Error("CanceledError string mismatch")
}

realErr := hideErrInterfaceType(e)
if !errors.Is(realErr, CanceledError{}) {
t.Error("CanceledError 'Is' broken")
}
if !errors.Is(realErr, innerErr) {
t.Error("CanceledError 'Is' broken")
}
if errors.Unwrap(e) != innerErr {
t.Error("CanceledError 'Unwrap' broken")
}
}

func TestAsyncError(t *testing.T) {
e := AsyncError{}
innerErr := errors.New("async")
if e.Error() != "async started" {
t.Error("AsyncError string mismatch")
}
e.Err = errors.New("async")
e.Err = innerErr
if e.Error() != "async started with error: "+e.Err.Error() {
t.Error("AsyncError string mismatch")
}

realErr := hideErrInterfaceType(e)
if !errors.Is(realErr, AsyncError{}) {
t.Error("AsyncError 'Is' broken")
}
if !errors.Is(realErr, innerErr) {
t.Error("AsyncError 'Is' broken")
}
if errors.Unwrap(e) != innerErr {
t.Error("AsyncError 'Unwrap' broken")
}
}

func TestInternalError(t *testing.T) {
Expand All @@ -90,3 +126,7 @@ func TestInternalError(t *testing.T) {
t.Error("InternalError string mismatch")
}
}

func hideErrInterfaceType(err error) error {
return err
}