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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









