qmuntal / stateless

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

stateless.GetTransition can panic #36

Closed rledisez closed 2 years ago

rledisez commented 2 years ago

https://github.com/qmuntal/stateless/blob/d97731633b8155699d8c14872789ba1702ac1376/config.go#L14-L18

If GetTransition is passed a context without transition value, ctx.Value() returns nil, and the type assertion fail, resulting in a panic. It does not match the documentation of the function.

panic: interface conversion: interface {} is nil, not stateless.Transition [recovered]
    panic: interface conversion: interface {} is nil, not stateless.Transition

goroutine 594 [running]:
testing.tRunner.func1.2({0xa59e00, 0xc000d9a9c0})
    /usr/local/go/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
    /usr/local/go/src/testing/testing.go:1212 +0x218
panic({0xa59e00, 0xc000d9a9c0})
    /usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/qmuntal/stateless.GetTransition(...)
    /root/go/pkg/mod/github.com/qmuntal/stateless@v1.4.0/config.go:17
qmuntal commented 2 years ago

Thanks for reporting this!