Using erlports and jaxon to grab youtube metadata via python -- handle_info/2 FunctionClauseError

With the genserver code I’m calling a python script. The script returns back asynchronously a large json blob to stdout.

What’s not clear to me is why there an error after the successful call to handle_info, ** (FunctionClauseError) no function clause matching in GrabAdapter.handle_info/2

Any insight into this? Is there a better way to do this than ports? I’ve noticed that if the JSON returned is ever malformed, The GenServer crashes and never comes back (“BROKEN PIPE”)

iex(2)> GrabAdapter.start_link("https://www.youtube.com/watch?v=ABuNwLP-z9o")
{:ok, #PID<0.518.0>}
Received message from port: {"id": "ABuNwLP-z9o", "uploader": "Phan Trieu", "uploader_id": "trieuphan1", "uploader_url": "http://www.youtube.com/user/trieuphan1", "channel_id": "UCG0SzK_t4-Ylf1yZq9Xmi_g", "channel_ur...
...wed ! Gaming Music | The Best of All Time | 2020", "_filename": "\ud83d\udd25 Top 50 NoCopyRightSounds _ Best of NCS _ Most viewed ! Gaming Music _ The Best of All Time _ 2020-ABuNwLP-z9o.mp4"}

title: 🔥 Top 50 NoCopyRightSounds | Best of NCS | Most viewed ! Gaming Music | The Best of All Time | 2020
iex(3)> [error] GenServer #PID<0.518.0> terminating
** (FunctionClauseError) no function clause matching in GrabAdapter.handle_info/2
    (grabber 0.1.0) lib/grab_adapter.ex:29: GrabAdapter.handle_info({#Port<0.19>, {:exit_status, 0}}, %{port: #Port<0.19>, ytchannel: "https://www.youtube.com/watch?v=ABuNwLP-z9o"})
    (stdlib 3.11) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib 3.11) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib 3.11) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: {#Port<0.19>, {:exit_status, 0}}
State: %{port: #Port<0.19>, ytchannel: "https://www.youtube.com/watch?v=ABuNwLP-z9o"}
** (EXIT from #PID<0.515.0>) shell process exited with reason: an exception was raised:
    ** (FunctionClauseError) no function clause matching in GrabAdapter.handle_info/2
        (grabber 0.1.0) lib/grab_adapter.ex:29: GrabAdapter.handle_info({#Port<0.19>, {:exit_status, 0}}, %{port: #Port<0.19>, ytchannel: "https://www.youtube.com/watch?v=ABuNwLP-z9o"})
        (stdlib 3.11) gen_server.erl:637: :gen_server.try_dispatch/4
        (stdlib 3.11) gen_server.erl:711: :gen_server.handle_msg/6
        (stdlib 3.11) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

You are receiving a message {port, {:exit_status, code}} which you aren’t handling hence the exception. The exception messages are really quite good and in this case tell you exactly why the genserver crashed.

Thanks, yes that is the issue for sure.

If I wanted to catch this particular exception(exit_status), where does OTP allow me to? If I do this in handle_info():

    try do
      title =
      stream |> Jaxon.Stream.from_enumerable() |> Jaxon.Stream.query([:root, "title"]) |> Enum.to_list()
      IO.puts "title: #{title}"
    catch
      :error, _ -> IO.puts "there was an error with no message"
      {:error, message} -> IO.puts "there was an error: #{message}"
      {:exit_status, exit_status} -> IO.puts "posix process exited: #{exit_status}"
    end

The exception still occurs.

The “easy fix” would be to do this:

port = Port.open({:spawn, cmd}, [:binary])
instead of:
port = Port.open({:spawn, cmd}, [:binary, :exit_status])

and fully ignore exit_status but in the interests of learning, what callback should capture the original Port.open() call(putting the try-catch in handle_continue() did not seem to help)?

The exception occurs because a message is being delivered to your GenServer that you do not handle. You need a handle_info clause that matches what @kip said. You don’t need a try/rescue

1 Like