Handling GenServer exception written in Erlang

Trying to pattern match against a GenServer exception, however the error message is written in Erlang:

2021-12-22T09:33:56.601136+00:00 error: crasher: initial call: 'Elixir.MyGenServer':'-handle_info/2-fun-0-'/0, pid: <0.3813.873>, registered_name: [], exit: {{timeout,{gen_server,call,[py_pool,{checkout,#Ref<0.2885861564.3326083073.228601>,true},60000]}},[{gen_server,call,3,[{file,"gen_server.erl"},{line,247}]},{poolboy,checkout,3,[{file,"/home/MyApp/deps/poolboy/src/poolboy.erl"},{line,63}]},{poolboy,transaction,3,[{file,"/home/MyApp/deps/poolboy/src/poolboy.erl"},{line,82}]},{'Elixir.Task.Supervised',invoke_mfa,2,[{file,"lib/task/supervised.ex"},{line,90}]},{'Elixir.Task.Supervised',reply,5,[{file,"lib/task/supervised.ex"},{line,35}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]}, ancestors: ['Elixir.MyGenServer',<0.388.0>,'Elixir.MyApp.Supervisor',<0.335.0>], message_queue_len: 0, messages: [], links: [<0.389.0>], dictionary: [{'$callers',[<0.389.0>]}], trap_exit: false, status: running, heap_size: 17731, stack_size: 29, reductions: 31491; neighbours:

My attempt:

defmodule MyGenServer do
  use GenServer
  require Logger

  @impl true
  def handle_info(error, state) do
    Logger.error(error)
    {:noreply, state}
  end
end

How should handle_info be modified to catch this exception?

{timeout,{gen_server,call,[py_pool,{checkout,#Ref<...>,true},60000]}

The handle_info in the message is a bit of a red herring - this is an exit with reason :timeout from the :poolboy instance named py_pool.

To capture this in your GenServer, you’d need to set trap_exit with Process.flag/2 and then handle the {:EXIT, pid, reason} message in handle_info.

3 Likes