qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
898 stars 47 forks source link

No way to get error returned from PermitDynamic #49

Closed lexuzieel closed 1 year ago

lexuzieel commented 1 year 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 1 year ago

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