Hello,
I’m wondering if anyone can help me with my problem.
I have the following LiveView code (this is just an example to show the problem):
defmodule LiveViewErrorsWeb.PageLive do
use LiveViewErrorsWeb, :live_view
def mount(_, _, socket) do
Process.send_after(self(), :foo, 10000)
{:ok, socket}
end
def handle_info(:foo, socket) do
nil.foo()
{:noreply, socket}
end
end
On mount, it sends itself a message in 10 seconds. The handler for that message throws an error, causing the process to die. LiveView helpfully tries to reconnect, but since mount
is then called again, in another 10 seconds the same error happens again.
In our real life scenario, we have some old data in the database, which doesn’t have the required fields that newer data does. So when someone goes to view one of these old records, we load the data but the rendering fails because one or more fields is not present. Obviously we can just fix the bug, but the problem is that whilst we haven’t fixed all of the bugs, if the user then leaves their browser window open, we get an infinite stream of error messages in the logs, from the LiveView failing and the reconnecting.
This pollutes the logs, and is also causing us issues with error tracking software where a single person leaving a LiveView tab open can fill up our quota of errors quite quickly.
I’ve looked into various ways around this, but I can’t seem to figure out a way to handle it nicely, whilst also preserving the useful feature that the LiveView will reconnect if there is a network error, or a redeployment on the server side, etc.
Does anyone know if this is possible? The only thing I can currently think of is to write a hook in Javascript that would check if we’ve reconnected more than N times in the last T time period. If so, I could somehow stop it from reconnecting. However, it would be nicer if I could do this in Elixir in the mount
function, so that I could then show an error message instead of running the error-producing code again.
Thanks for any help you can give!
Steve