xward

xward

Exception management in liveview render good practice

Hello !

The goal is to have all exception that occurs and send it to an external service, like sentry.

I managed to catch them for:

  • exceptions when user is not connected
  • exceptions during mount, liveview handle_event, live_component handle_event and handle_params

But I can’t catch when:

  • exceptions during render

I could move all my heex into a render function, and manual catch for each render, but it would be very unpleasant.

I had hard time finding anything about good practice about it with liveview, I feel like I’m missing something obvious. My current approach is not good since it is very easy to forget something.

Anyone had the same trouble and found a graceful way of managing this ?

Have a nice day !

Marked As Solved

xward

xward

Some final feedbacks:

Tried using the “catch all” from sentry, but it catch way too much thing, (if a render fail, I have multiple Logger.error that occurs (one from phoenix’s GenServer, the one from raise, etc…).

The sentry became polluted very fast on any Logger.error.

My current solution: write my own little Logger backend :

defmodule MyApp.SentryBackend do
  @behaviour :gen_event

  def init(__MODULE__), do: {:ok, nil}

  def handle_call({:configure, _options}, state), do: {:ok, :ok, state}

  # only Logger errors with crash_reason
  def handle_event({level, _gl, {Logger, _msg, _ts, meta}}, state) when level in [:emergency, :alert, :critical, :error] do
    if meta[:crash_reason] do
      {reason, stacktrace} = meta[:crash_reason]
      Sentry.capture_message(inspect(reason), stacktrace: stacktrace)
    end

    {:ok, state}
  end

  def handle_event(_, state), do: {:ok, state}

  def terminate(_reason, _state), do: :ok
end

I allows my to further filter if I need to.

cheers !

Also Liked

xward

xward

That’s a fair point ! will give it a try. Thanks.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement