How to detect errors in :gen_statem?


I wrote few :gen_statem processes, but when I want to test them if there is some bug in the code for example FunctionClause errors, iex don’t throw like it will do with a GenServer.

Did you ever find this issue and how did you fix it ?


Try going to hex and downloading gen_state_machine which provides a more Elixir like interface to the gen_statem OTP behaviour. This might help. Otherwise shouldn’t the FunctionClause error tell you in which function the error occurred?


The fact is if I don’t trap exit or monitor the process I don’t catch the error .
But thanks for the tip

If you are doing gen_statem:call then you will see if there is an error. But if you do an gen_server:cast then it is like an erlang/elixir send where you don’t get any default error notification.

Thanks. I will try it.

So it is preferable to use call generally speaking as it gives some acknowledgement ?

To be a bit picky here :grin: you use call when the semantics of what you are doing is synchronous and there is a reply to your request which you wish to receive or maybe if you need to wait until it has been done. Again it’s the semantics which decide. If you just want to “tell” the server/state machine something then you would do a cast.


If you only need an acknowledgement of something ongoing but do not care about the result, you can do a call and inside the callback insert an event to do the processing afterwards and return immediately.

This is similar to the continue concept in gen_server.