qmuntal / stateless

Go library for creating finite state machines
https://wikipedia.org/wiki/Finite-state_machine
BSD 2-Clause "Simplified" License
972 stars 53 forks source link

No way to get error returned from PermitDynamic #49

Closed lexuzieel closed 2 years ago

lexuzieel commented 2 years ago

It appears that currently there seems no way to get the error returned from PermitDynamic.

In my code I return an error using the following code:

fsm.Configure("prompt").
PermitDynamic("step", func(ctx context.Context, args ...interface{}) (stateless.State, error) {
    code := args[0].(string)
    if code != "bar" {
        return "prompt", errors.New("invalid code")
    }

    return "apply", nil
})

in my test I fire a transition:

err := fsm.Fire("step", "foo")
assert.ErrorContains(t, err, "invalid code")

and get:

Error "stateless: Dynamic handler for trigger step in state prompt has failed" does not contain "invalid code"

I suppose this is a bug and when firing a transition, an error should be overwritten?

qmuntal commented 2 years ago

Thanks for reporting this bug. I'll fix it in the next patch release.