Infinite loop in a LiveView when a Repo.all fails in a handle_params or mount

I have a LiveView where I fetch a list of data for the page in the handle_params callback, although this situation will happen in a mount too.

If a Repo.all call errors, my LiveView goes into a very tight loop and the web page reloads over and over again very quickly. The exact reason it fails in my situation is because I am querying by a UUID and I had a malformed one.

I can fix this by checking the UUID first, and I think I’ll make my own wrapper for Repo.all that wraps it in a try/rescue, but I’m super curious about what’s going on here. I couldn’t reproduce it with a raise or a throw in the handle_params.

I’m a newer Elixir developer so there might be a major misunderstanding on my part.

Could you post us at the very minimum your mount and handle_params code blocks? Are you saying in the database you have bad data so the return of Repo.all errors out?

It’s not bad data, but rather a bad parameter. I’m going to try to build a simple app to reproduce it this weekend and I’ll post back. That will give me something to share, but I guess if I can’t reproduce it separately than I need to review what I’m doing wrong in my regular code.

Our primary keys are UUIDs. Imagine this call:

Repo.all(from m in MyModule, where: m.my_module_id == ^something_that_is_not_a_uuid)

When this fails because I didn’t check my UUID properly, Ecto throws a %Ecto.Query.CastError{}, which seems to throw my LiveView into a tailspin.

I see, Ecto.UUID.cast/1 might be helpful to guard against invalid UUIDs

1 Like

I see, Ecto.UUID.cast/1 might be helpful to guard against invalid UUIDs

Once I realized what the issue was I put this check in but I guess to avoid other things that I can’t think of I should be wrapping these in try/rescue blocks.

Thanks for your replies!

Which version of LiveView are you using? We have changed the way we handle errors during connected mount in the last rc to prevent issues like these :slight_smile:

1 Like

Which version of LiveView are you using? We have changed the way we handle errors during connected mount in the last rc to prevent issues like these :slight_smile:

We’re still on 0.20.17 but I’m in the middle of trying to upgrade to the RC. We have a complex LiveComponent that worked with phx-feedback-for but is now broken. I have everything else in the app working with Phoenix.Component.used_input?. I need to find time to sit down and figure out what’s wrong. For some reason the shim didn’t work in this situation.

That’s great to hear that this may be different in the latest RC. Thanks for letting me know.