Why handle_info does not get called

Hi all

I create a GenServer and implemented the behaviour that looks like:

  # Server callbacks
  def init(_state) do
    {:ok, Application.get_env(:odata_service, :url)}
  end

  def handle_call({:get, bUri, bUser, bPassword, mHttpHeaders}, _from, state) do
    {:reply, request(:get, state <> bUri, bUser, bPassword, mHttpHeaders), state}
  end

  def handle_call({:get, bUri, bUser, bPassword}, _from, state) do
    {:reply, request(:get, state <> bUri, bUser, bPassword), state}
  end

  def handle_info(_msg, state) do
   IO.inspect("Foooooo")
   {:noreply, state}
  end

Then I start the server

iex(1)> {:ok, pId} = GenServer.start_link(SapOdataService.Worker, :ok)
{:ok, #PID<0.311.0>}

Call something does not exist:

iex(2)> GenServer.call(pId, {:post, "test"})                          
** (EXIT from #PID<0.308.0>) an exception was raised:
    ** (FunctionClauseError) no function clause matching in SapOdataService.Worker.handle_call/3
        (sap_odata_service) lib/sap_odata_service/worker.ex:30: SapOdataService.Worker.handle_call({:post, "test"}, {#PID<0.308.0>, #Reference<0.0.3.357>}, "http://services.odata.org/V4/Northwind/Northwind.svc/")
        (stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
        (stdlib) gen_server.erl:647: :gen_server.handle_msg/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

And got as show above and error message. Why the handle_info function does not get call?

In doc, it says:

handle_info(msg, state) - invoked to handle all other messages which are received by the process.

I am doing something wrong.

Thanks

Hi @kostonstyle,

The handle_info callback is for other random messages that don’t originate from GenServer.call or GenServer.cast, so eg:

iex(2)> {:ok, pid} = GenServer.start_link(SapOdataService.Worker, :ok)
{:ok, #PID<0.66.0>}
iex(3)> send pid, :hello
"Foooooo"
:hello 
2 Likes

Note that the Elixir convention for variable names, just like for function names, is generally to use snake_case, consisting of lowercase letters and underscores. It certainly doesn’t matter for your own code of course, but if you intend to share / publish code, it may be worth keeping in mind :slight_smile:

1 Like

Thanks so much.