binarypaladin
"Catch-all" error handling in LiveView
In my total experience with LiveView, something that’s either a matter or personal ignorance or just problematic with how it works is generic error handling.
In the normal request/response cycle on a controller, catch-alls are pretty easy. We’ve got action_fallback to handle that and another controller out there dealing with errors.
I can’t see any way that this works nicely with LiveView in some sort of nice, transparent way. (Mind you, it’s quite possible someone provides an answer where I’m like, “D’oh!”)
To me, it’s looking like my main option would be to use on_mount for certain expected errors and then wrap other expected errors in helper functions that expect certain errors. That’s doable, but it seems like a repeat of assign_defaults in a different context.
There’s also one more issue:
While LiveView will try to recover by reseting state, most of the time the errors are not ephemeral. If the user does a thing that resulted in an error they’ll try again. We’re using Honeybadger for collecting these errors and I like presenting the user with, “Hey, provide this ID when contacting support.” A catch-all + flash or something seems like a workable solution here.
Is there something in the lifecycle I’m just missing and a sort of “blessed” way to do this? If not, what are some patterns that others have used for similar scenarios?
Most Liked
mayel
Just came across this old thread and wanted to notify you that I’ve since implemented it in a transparent way that doesn’t require repeating code in views or live components, by defining it in our Web module with @before_compile at bonfire_ui_common/lib/web.ex at main · bonfire-networks/bonfire_ui_common · GitHub and then bonfire_ui_common/lib/web.ex at main · bonfire-networks/bonfire_ui_common · GitHub
defp live_mount_before_compile(env) do
if Module.defines?(env.module, {:mount, 3}) do
quote do
defoverridable mount: 3
def mount(params, session, socket) do
undead_mount(socket, fn ->
super(params, session, socket)
end)
end
end
end
end
mayel
I’m sure it could be done transparently with macros but there’s something to be said for opt-in explicitness, but it’s not as much of a hassle as you expect, instead of wrapping all LiveView function bodies in undead, we do this:
def handle_event(action, attrs, socket), do: Bonfire.UI.Common.LiveHandlers.handle_event(action, attrs, socket, __MODULE__, &do_handle_event/3)
and define do_handle_event/3 for view or component-specific logic, whilst LiveHandlers module takes care of error handling (and as a bonus also handles or delegates events that are used in more than one view or component).
binarypaladin
This is excellent. Thank you for sharing this. Although you might be the person I blame if I’m up later than I’d planned, lol.







