Get "Last Message" during rescue inside a GenServer

There is a very useful error log that prints if I don’t recuse an exception from within a GenServer.

GenServer {[GENSERVER NAME HERE]} terminating

I’ve dug around the elixir code base but can’t find how {'** Generic server ' ++ _, [name, last, state, reason | client]} is getting set in elixir/lib/logger/lib/logger.ex. Most importantly I want last (which is [ARGS TO THE HANDLE CALL HERE]) for my own logging purposes. Hoping someone has a way to access this in the recuse block of a try and not an answer that involves somehow storing the message args from the handle_call function…

1 Like

There is no other way than storing that message on your own. gen_server does exactly that internally.

Question is - what do you need that for?

I want it for custom logging. Also, in some of the cases it makes sense to return a reasonable response to the client instead of “let if fail”, hence the try/rescue.

The state is of a gen_server does not get updated until you reply with the new state, so storing anything will get lost if an exception occurred before that reply. I would either have to tuck it away in another process/ets or pass it around through out the internal call-stack. But, Logger has access to it to print to the screen, so it must be possible to know the last (current) mailbox message with the args.

Again, this is for logging and convenience, not a primary pattern for passing data around or performing business logic.

There is no other way than storing that message on your own somehow. If you want to show this as a part of log message then you put that information into logger metadata.

The gen_server loop in highly simplified form does exactly that (in Elixir for clarity):

def loop(state) do
  msg =
    receive do
      message -> message

  new_state =
    try do
      handle_message(message, state)
      kind, value ->
          :logger.error(%{last_msg: msg, kind: kind, value: value})
          :erlang.raise(kind, value, __STACKTRACE__)

1 Like

Thanks, hauleth, that was helpful!