Skip to content

Instantly share code, notes, and snippets.

@TylerPachal
Last active November 9, 2017 18:54
Show Gist options
  • Save TylerPachal/a1519a64d4fd46a040918a98a790b6f3 to your computer and use it in GitHub Desktop.
Save TylerPachal/a1519a64d4fd46a040918a98a790b6f3 to your computer and use it in GitHub Desktop.
Example usage of the Parser module, with exit trapping so a dying Parser will not kill the parent iex process
# Create a new Parser process
iex(1)> {:ok, pid} = Parser.start_link()
{:ok, #PID<0.112.0>}
# Figure out our iex process' PID
iex(2)> self()
#PID<0.110.0>
# See that our iex process is linked to the Parser process
iex(3)> Process.info(self(), :links)
{:links, [#PID<0.112.0>]}
# See that the reverse is also true
iex(4)> Process.info(pid, :links)
{:links, [#PID<0.110.0>]}
# Instruct the iex process to trap exit messages
iex(5)> Process.flag(:trap_exit, true)
false
# Send the Parser process a valid message
iex(6)> GenServer.call(pid, "100")
100
# Send the Parser process an invalid message
iex(7)> GenServer.call(pid, "tyler")
** (exit) exited in: GenServer.call(#PID<0.112.0>, "tyler", 5000)
# Stacktrace here
# Confirm the iex process still has the same PID and didn't restart
iex(8)> self()
#PID<0.110.0>
# See all of the messages the iex process has in its mailbox
iex(9)> flush()
{:EXIT, #PID<0.112.0>,
{:badarg,
[{:erlang, :binary_to_integer, ["tyler"], []},
{Parser, :handle_call, 3, [file: 'lib/parser.ex', line: 9]},
{:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 636]},
{:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 665]},
{:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}}
:ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment